Change $home for tomcat only? - java

I have two instances of tomcat that run under the same user in a test/dev environment. In each instance there is an application that writes configuration data to the same directory in the $home of the user that runs tomcat.
I'd like to set one instance of tomcat so that it thinks $home is somewhere else. Is this possible? If I do export home=foo/bar when I run one instance, will that affect the other instance?
Obviously I know that I can run each instance as a different user, I do not want to do that in this case.

Just create a file "setenv.sh" for both instances
{TOMCAT1_BASE_DIR}/bin/setenv.sh:
export home=/foo/bar1
{TOMCAT2_BASE_DIR}/bin/setenv.sh:
export home=/foo/bar2
On startup tomcat will load environment variables defined in setenv.sh

You can overwrite a variable for a specified command without exporting it this way:
HOME=/home/user command.sh
The HOME variable will be changed for command.sh but will still be your usual home everywhere else.

Edit {TOMCAT_INSTANCE}/bin/setenv.sh to set all required environment variables per instance.

Related

Java Spring project environment property file for both Linux and Window

I have a java spring project that contains property files. In the file, there are lots of folder path definitions. But these path are Linux paths. i.e. /home/share/Document. I would like to develop the project under both Linux and Windows so I have created a network share that I can access Linux file under Window environment. However, I have to add a prefix in front of the Linux path
such as \network\...\home\share\Document in order to make the path work.
What is the standard/normal way to handle this? I can image I should have two environment property files. For example, one is called DEV_unix.properties and the other is call DEV_win.properties. But this doesn't look perfect.
Could someone share his/her insights please ?
You can use profile in this case. Set active profile in your application.{profile}.properties. You can set this profile as a environment variable or VM arguments so that you need to set the environment variable in different machines based on the Operating system. You can check the below link for more details.
Springboot not loading application.dev.properties file

Using System Path Variable in Maven project - Eclipse

PROBLEM: Hello everyone! I have created a Maven project in which i am building
a RESTful API, now i have created few .json files in which i want
to store my JSON data in src/main/resources , but as i can see
every time i make a change ( example: register a user - write in a
file ) all changes get lost whenever i redeploy the WAR file or the
server gets restarted/reloaded.
Can someone please tell me how to use system PATH variable to fix
my problem? I want to store files somewhere i will be able to update
them without having to worry if server is being reloaded or not. Thank you in advance!
SMART WAY, with java configuration and classpath (unix example, launch your application this way)
java -classpath $PATH <your-class-name>
or
setenv CLASSPATH = $PATH
see unix and windows instructions.
BAD WAY, within application code
You can read the PATH variable from the OS environment calling System.getenv() and then looking for the "PATH" elements within the returned map.
Then you have to parse the string (e.g. with StringTokenizer) obtained to get each folder within the PATH variable
you have to look for your JSON files in each of the folder from previous point

Intellij IDEA: run/debug java application with a set of environment variables

My java application uses environment variables (System.getenv), and this set of variables is large and defined in a file like:
export VAR1="VAL1"
...
export VAR100500="VAL1500"
when i start the application from command line i use something like this:
source ../.config/.secret-secret && java -jar build/libs/app-name.jar
and the values are exported right before the launch (actually, the source command may be executed only once per session, since the values are exported)
But when i start the application from IDEA, reasonably those values are not exported to the run environment.
So, the question is: how can i export a set of environment variables to application launch environment in IDEA?
So, what i've tried already:
in Run/Debug Configurations -> Before Launch i started a shell
script which exports those variables (i use the same script to run
the app from command line). Script executed successfully, but looks
like the script is executed in different environment from the
application itself, so values are exported (and printed to IDEA run
console), but the application still can't read them from
System.getenv
i can't easily use Run/Debug Configurations -> Environment Variables, because there are a lot of variables to set up manually, also they are changed from time to time.
It's not enough to run entire IDEA with those variables, because, as i mentioned above, the values are changed often and should be re-read almost every launch.
I can tell you that editing the workspace.xml file (which is where the environment variables entered in the run configuration are stored) directly does not work, so don't bother trying that.
I think you are going to have to write a plugin that parses your script and updates the run configuration. I haven't written a plugin to do this myself so can't give you a fuller answer. You can see the relevant plugin in documentation here

using a 'environment' variable in java servlet running in tomcat container

I need to access a variable created in a shell script from a java servlet.
After reading the related posts in this forum I still haven't figured it out.
If I export a variable on my debian system (export var=foo123) and then I want to use
this variable in my sevlet (System.getenv(var)) it always returns a null value.
I have already tried adding 'export var=foo123' in /etc/init.d/tomcat7 but to no succes.
I think that you don't need to use export, it should be enough to put:
VARIABLE_NAME="variable_value"
in tomcat script file. And don't forget the ""!

PHP Environment variable

I am using PHP Java Bridgeon a windows and linux server. Now I want to modify the
CLASSPATH. When I do a phpinfo(), I see a CLASSPATH under environment variables section and
when I output the java.lang.system properties there is a property called java.class.path.
First of all what is the difference between the two? How do I modify both of them? Can they be modified programatically ?
I don't know about the phpinfo() information, but the System.property can't be set (to any effect) directly. It is effectively read-only and taken from the environment CLASSPATH variable and -classpath command line option when Java is launched.
You can effectively change the classpath used to load classes by creating your own classloaders or class loader instances (see URLClassLoader) and using it to load from dynamic locations.

Categories