Define environment variables in Tomcat

Sometimes you want to change the behavior of your code without recompiling / deploying.  A way to do this is defining environment variables that you can change at any time. A typical use case is for database connection properties (username, password, port, etc).

In your Tomcat home create this file $TOMCAT_HOME/bin/ and define variables like this:

export VAR1=my_var1_value
export VAR2=my_var2_value

Give execution permission

chmod 755

In Windows define variables like this:

set VAR1=my_var_value

After saving the file you must reboot Tomcat.

In your code you get the environment variable this way:

//value will be null if VAR1 does not exist
String value = System.getenv("VAR1");

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s