Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 714
mark Avatar asked Dec 22 '11 21:12

mark


People also ask

How do I run Tomcat 9 as a service in Windows?

To install an instance, first set the CATALINA_HOME environment variable to the name of the Tomcat installation directory. Then create a second environment variable CATALINA_BASE and point this to the instance folder. Then run "service. bat install" command specifying a service name.

How do I run Tomcat 8 as a service in Windows?

1) Open a command prompt, go to the bin folder of the apache tomcat and run "service. bat install". If the CATALINA_HOME, JAVA_HOME and JRE_HOME are correctly configured, the 'Tomcat' service will be installed successfully.


2 Answers

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.

like image 118
prunge Avatar answered Oct 21 '22 17:10

prunge


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

like image 24
Andrew Kew Avatar answered Oct 21 '22 15:10

Andrew Kew