如何将系统属性传递给托管在作为 Windows 服务运行的 Tomcat 7 中的 Web 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8609998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:43:38  来源:igfitidea点击:

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

javawindowstomcat

提问by mark

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 setstatements in the setenv.batfile inside the tomcat's bin folder
  1. 通过 CATALINA_OPTS
  2. 通过 JAVA_OPTS
  3. 通过 TOMCAT_OPTS
  4. 通过将set语句放在 tomcat 的 bin 文件夹内的setenv.bat文件中

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

我的问题是,我已经尝试了上述所有方法,但我的 Web 应用程序仍然看不到我的系统属性!

Here is what I am doing:

这是我在做什么:

  1. Stop tomcat7 service
  2. set CATALINA_OPTS=-Dabc.def=truein the system environment
  3. set JAVA_OPTS=-Dabc.def=truein the system environment
  4. set TOMCAT_OPTS=-Dabc.def=truein 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_OPTSand JAVA_OPTSand TOMCAT_OPTSequal to -Dabc.def=true
  8. run my web app, which is a simple servlet dumping all the system properties to the response stream - abc.defis not amongst them
  1. 停止tomcat7服务
  2. CATALINA_OPTS=-Dabc.def=true在系统环境中设置
  3. JAVA_OPTS=-Dabc.def=true在系统环境中设置
  4. TOMCAT_OPTS=-Dabc.def=true在系统环境中设置
  5. 将上述所有内容放入c:\Program Files\Apache Software Foundation\Tomcat 7.0\bin\setenv.bat(似乎完全多余,但以防万一)
  6. 启动tomcat7服务
  7. 使用 Process Explorer 工具检查 tomcat7 进程的环境 - 环境是正确的,我确实看到了CATALINA_OPTSandJAVA_OPTSTOMCAT_OPTSequal to-Dabc.def=true
  8. 运行我的 web 应用程序,这是一个简单的 servlet,将所有系统属性转储到响应流 -abc.def不在其中

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

请让我摆脱痛苦,告诉我该怎么做。

回答by prunge

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

对于 Tomcat 服务,启动设置存储在注册表下的注册表Options项中:

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

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

(substitute appropriate Tomcat version where needed).

(在需要时替换适当的 Tomcat 版本)。

Edit:

编辑:

On 64-bit Windows, the registry key is:

在 64 位 Windows 上,注册表项是:

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

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.

即使 Tomcat 在 64 位 JVM 下运行。