mark
mark

Reputation: 62876

How to pass a system property to a web application hosted in Tomcat 7 running as a Windows Service?

There are numerous places on the Internet, suggesting that it is easily achieved by any (or all) of the following methods:

  1. through CATALINA_OPTS
  2. through JAVA_OPTS
  3. through TOMCAT_OPTS
  4. by placing the set statements in the setenv.bat file inside the tomcat's bin folder

My problem, is that I have tried all of the above and my web application still does not see my system property!

Here is what I am doing:

  1. Stop tomcat7 service
  2. set CATALINA_OPTS=-Dabc.def=true in the system environment
  3. set JAVA_OPTS=-Dabc.def=true in the system environment
  4. set TOMCAT_OPTS=-Dabc.def=true in the system environment
  5. put all of the above into c:\Program Files\Apache Software Foundation\Tomcat 7.0\bin\setenv.bat (seems totally redundant, but just in case)
  6. Start tomcat7 service
  7. Inspect the environment of the tomcat7 process using the Process Explorer tool - the environment is correct, I do see both CATALINA_OPTS and JAVA_OPTS and TOMCAT_OPTS equal to -Dabc.def=true
  8. run my web app, which is a simple servlet dumping all the system properties to the response stream - abc.def is not amongst them

Please, put me out of my misery and tell me how to do it.

Upvotes: 6

Views: 7221

Answers (2)

Andrew Kew
Andrew Kew

Reputation: 3363

I know this post is almost 9 years old but thought someone might find this useful.

Although @prunge and @mark answers were very accurate and following their logic I was able to add system properties to my tomcat 7 instance running on Windows, there is an easier way.

In the installation directory of Tomcat there is an exe you can run called

%INSTALL_DIRECTORY%\bin\tomcat7w.exe

This opens up a Tomcat properties windows where you can control the service i.e. start and stop tomcat and there is a tab (Java) that allows you to set the Java properties as well

enter image description here

Scroll to the end of that panel under "Java Options" and add your system properties

-Dpropertyname=value

Then navigate back to the General tab and restart tomcat

I have tested this and my grails app now can see my properties. I use the following Groovy code to get the property out

System.properties.getProperty("propertyname")

Adding the system properties in the Windows registry showed up in this window as well so its one and the same thing, just this application seems to me to be slightly more convenient.

Hope this helps someone else

Upvotes: 0

prunge
prunge

Reputation: 23268

For the Tomcat service, startup settings are stored in the registry under Options key at:

HKEY_LOCAL_MACHINE\SOFTWARE\Apache Software Foundation\Procrun 2.0\Tomcat<X>\Parameters\Java

(substitute appropriate Tomcat version where needed).

Edit:

On 64-bit Windows, the registry key is:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\Tomcat<X>\Parameters\Java

even if Tomcat is running under a 64-bit JVM.

Upvotes: 9

Related Questions