Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting User Environment Variables for tomcat on Windows

I am running tomcat 8 on windows 2012.

I have a webapp that needs an user environment variable to be set in order to run.

I can start tomcat and retrieve the variable successfully like this:

set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_65
set CATALINA_HOME=C:\TOOLS\apache-tomcat-8.0.30

set CATALINA_BASE=C:\Users\ULUser\tomcat\myapp
set MY_VAR=%CATALINA_BASE%
set PATH=%CATALINA_ROOT%\bin;%PATH%
set CATALINA_OPTS="-Djava.security.properties=%CATALINA_BASE%\conf\java.security.properties"

%CATALINA_HOME%\bin\catalina.bat run

But when I run as a windows service I cannot retrieve the MY_VAR variable using System.getenv("MY_VAR");

Googling has not helped because all the pages show how to set the Java and Tomcat variables, but not how to set user variables.

I cannot set it for the entire machine because I have several instances of tomcat and each one needs a different environment variable.

I would prefer a method that involves setting the variable in the tomcat configuration rather that with windows configuration.

My alternative is to use context.xml file in tomcat and retrieve the value with logic that retrieves context settings.

I originally posted this on superuser, but it was suggested that stackoverflow is a better place for this question. If I get good answers here I will delete it off of superuser.

like image 281
Be Kind To New Users Avatar asked Nov 07 '16 02:11

Be Kind To New Users


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. bat (on Windows) or setenv.sh (on *nix). The file has to be readable.

How do I connect to Tomcat on Windows?

Access the Apache Tomcat console by browsing to http://localhost:8080/ (if installed as a non-root user) or http://localhost/ (if installed as the root user).


1 Answers

Had the same problem and I've just managed to solve it.

From an admin command prompt do the following command:

tomcat8 //US//YourServiceName ++Environment varname=value

You can set more than one variable by separating them with a semicolon (;) or a hash (#). The documentation is quite clear, but it's just difficult to believe anything's happened, because there's no visual indication anywhere, meaning if you run:

tomcat8w //ES//YourServiceName 

to get the GUI config tool you will not see the environment variable anywhere, but if you run the service it will pick it up. It's like hidden magic. Don't know why they haven't included it somewhere in the config panel.

like image 79
hktegner Avatar answered Oct 03 '22 16:10

hktegner