I am using the eclipse for web application coding. Within this I passed environment variable like :
APP_MASTER_PASSWORD
and its value.I can access this value in java code as System.getenv("APP_MASTER_PASSWORD")
.
But now I want to pass this environment variable to tomcat and access it in application instead of passing thru eclipse.
So how can I pass such variable to tomcat?
I googled about it. But I didn't get any solution.
Thank you.
This question is related to
java
tomcat
web-applications
You can use setenv.bat or .sh to pass the environment variables to the Tomcat.
Create CATALINA_BASE/bin/setenv.bat or .sh file and put the following line in it, and then start the Tomcat.
On Windows:
set APP_MASTER_PASSWORD=foo
On Unix like systems:
export APP_MASTER_PASSWORD=foo
In case of Windows, if you can't find setenv.bat, in the 2nd line of catalina.bat (after @echo off) add this:
SET APP_MASTER_PASSWORD=foo
May not be the best approach, but works
DID NOT WORK >> Set as system environment variable: I had set the environment variable in my mac bash_profile
, however, Tomcat did not see that variable.
DID NOT WORK >> setenv.sh: Apache's recommendation was to have user-defined environment variables in setenv.sh
file and I did that.
THIS WORKED!! >> After a little research into Tomcat startup scripts, I found that environment variables set using setenv.sh
are lost during the startup. So I had to edit my catalina.sh
against the recommendation of Apache and that did the trick.
Add your -DUSER_DEFINED
variable in the run command in catalina.sh
.
eval exec "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
-classpath "\"$CLASSPATH\"" \
-Djava.security.manager \
-Djava.security.policy=="\"$CATALINA_BASE/conf/catalina.policy\"" \
-Dcatalina.base="\"$CATALINA_BASE\"" \
-Dcatalina.home="\"$CATALINA_HOME\"" \
-Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
-DUSER_DEFINED="$USER_DEFINED" \
org.apache.catalina.startup.Bootstrap "$@" start
P.S: This problem could have been local to my computer. Others would have different problems. Adding my answer to this post just in case anyone is still facing issues with Tomcat not seeing the environment vars after trying out all recommended approaches.
For Unix & Mac systems, Go to /bin/setenv.sh inside tomcat folder
Add the below line
export JAVA_OPTS="$JAVA_OPTS -DAPP_MASTER_PASSWORD=mypass"
Now System.getProperty("APP_MASTER_PASSWORD")
will return "mypass"
Environment Entries specified by <Environment>
markup are JNDI, accessible using InitialContext.lookup under java:/comp/env
. You can specify environment properties to the JNDI by using the environment parameter to the InitialContext constructor and application resource files.
System.getEnv()
is about system environment variables of the tomcat process itself.
To set an environment variable using bash command :
export TOMCAT_OPTS=-Dmy.bar=foo
and start the Tomcat :
./startup.sh
To retrieve the value of System property bar
use System.getProperty()
. System.getEnv()
can be used to retrieve the environment variable i.e. TOMCAT_OPTS
.
You should use System property instead of environment variable for this case. Edit your tomcat scripts for JAVA_OPTS and add property like:
-DAPP_MASTER_PASSWORD=foo
and in your code, write
System.getProperty("APP_MASTER_PASSWORD");
You can do this in Eclipse as well, instead of JAVA_OPTS, copy the line in VM parameters inside run configurations.
Environment variables can be set, by creating a setenv.bat
(windows) or setenv.sh
(unix) file in the bin
folder of your tomcat installation directory. However, environment variables will not be accessabile from within your code.
System properties are set by -D
arguments of the java process. You can define java starting arguments in the environment variable JAVA_OPTS
.
My suggestions is the combination of these two mechanisms. In your apache-tomcat-0.0.0\bin\setenv.bat
write:
set JAVA_OPTS=-DAPP_MASTER_PASSWORD=password1
and in your Java code write:
System.getProperty("APP_MASTER_PASSWORD")
If you are starting your Tomcat from Eclipse ("Servers" view) then you should have a "Run/Run Configuration" (menu) entry called "Apache Tomcat / Tomcat …". When you select this entry in the list of run configurations you get a window with several tabs, one of which is labeled "Environment". There you can configure environment variables for your Tomcat. Be sure to restart Tomcat afterwards.
Source: Stackoverflow.com