34

I am using the eclipse for web application coding. Within this I passed environment variable like :

  1. Project--> Run as --> Run Configuration. And selected Environment tab.
  2. Add new environment variable with name 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.

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
Naresh J
  • 2,087
  • 5
  • 26
  • 39
  • Are there no options using a Tomcat vhost? These environment settings are not handy if you have multiple copies of the app running. I believe they're called "environment entries"? – Wouter Apr 21 '14 at 09:00
  • On Tomcat 8.5.51 provided in Amazon Linux repos, I had to append the env-var declaration to `/usr/libexec/tomcat/preamble`; creating `$CATALINA_BASE/bin/setenv.sh` didn't work (in above distro `CATALINA_BASE=/usr/share/tomcat`) – Janaka Bandara Mar 04 '22 at 16:12

9 Answers9

57

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
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Shinichi Kai
  • 4,415
  • 2
  • 21
  • 25
  • 1
    this is the case specific for Ubuntu I believe. For Red Hat for example, you can edit CATALINA_HOME/tomcat7.conf – Don Cheadle Nov 13 '14 at 22:53
  • 2
    This is a little late but... According to [Tomcat's documentation](https://tomcat.apache.org/tomcat-7.0-doc/RUNNING.txt) , creating the setenv.sh should work for any flavor of Unix. The catalina.sh script will check to see if setenv.sh exists, if so, execute it during the startup process. – Delicia Brummitt Jul 05 '16 at 19:37
  • 2
    `catalina.sh` checks for both `CATALINA_BASE/bin/setenv.sh` and `CATALINA_HOME/bin/setenv.sh`, and executes them, if present. – rustyx Aug 15 '16 at 18:18
  • 2
    You can create a setenv.sh script, if it is not already there – Pratik Singhal Jun 04 '17 at 14:22
21

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.

Ankit
  • 3,083
  • 7
  • 35
  • 59
11

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.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
9

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")
slartidan
  • 20,403
  • 15
  • 83
  • 131
5

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"

IamVickyAV
  • 1,495
  • 1
  • 17
  • 15
1

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.

Renardo
  • 499
  • 4
  • 13
1

My recipe to fix it:

  1. 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.

  2. DID NOT WORK >> setenv.sh: Apache's recommendation was to have user-defined environment variables in setenv.sh file and I did that.

  3. 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.

Harish
  • 638
  • 1
  • 10
  • 20
0

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

ObviousChild
  • 119
  • 1
  • 9
0

I have gone through various solutions but none of them worked. Finally I found the tomcat documentation https://tomcat.apache.org/tomcat-7.0-doc/RUNNING.txt. You can use setenv.bat or .sh for passing the environment variables to the Tomcat. setenv will not be present by default, it should be created inside CATALINA_BASE/bin. Configure your environment variable like below in setenv.bat:

set "JRE_HOME=%ProgramFiles%\Java\jre6"