Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass user defined environment variable to tomcat

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.

like image 841
Naresh J Avatar asked Jun 10 '13 07:06

Naresh J


People also ask

Where are Tomcat environment variables stored?

Apart from CATALINA_HOME and CATALINA_BASE, all environment variables can be specified in the "setenv" script. The script is placed either into CATALINA_BASE/bin or into CATALINA_HOME/bin directory and is named setenv.

What should Catalina_home be set to?

The CATALINA_HOME environment variable should be set to the location of the root directory of the "binary" distribution of Tomcat. The Tomcat startup scripts have some logic to set this variable automatically if it is absent, based on the location of the startup script in *nix and on the current directory in Windows.

What is Setenv in Tomcat?

The setenv. batcommand is used to modify or to set environment variables for the Tomcat application server. These setting only apply when using the catalina.


2 Answers

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

like image 51
Shinichi Kai Avatar answered Sep 25 '22 23:09

Shinichi Kai


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.

like image 31
Ankit Avatar answered Sep 24 '22 23:09

Ankit