TOMCAT_OPTS、环境变量和 System.getEnv()

2022-01-15 00:00:00 environment-variables java tomcat maven

我使用tomcat,我想在我的java代码中获取一个环境变量.

I use tomcat and I want to get an environment variable in my java code.

要设置环境变量,我使用这个 bash 命令:

To set an environment variable, I use this bash command :

export TOMCAT_OPTS=-Dmy.var=foo

之后我启动tomcat

After it I start tomcat

./startup.sh (in bin folder of tomcat)

在我的 java 代码中,我尝试获取这个变量:

In my java code, I try to get this variable :

System.getEnv("my.var")

但它返回 NULL.

我该怎么做?

我准确地说,如果我使用 maven 启动 tomcat 并使用 eclipse 环境选项卡,则找到变量!但是我需要像上面那样在生产模式下启动tomcat.

I precise that if I use maven to launch tomcat and use eclipse environment tab, the variable is found ! But I need to launch tomcat like above in production mode.

直接使用 export MY_VAR 时,它在本地运行,但不在我的服务器上...

when using export MY_VAR directly it runs in local but not on my server...

推荐答案

终于在CATALINA_HOME中找到了一个名为tomcat6.conf的配置文件.我将 export my.var=foo 添加到文件末尾, System.getenv("my.var") 现在返回值...

I finally found a config file named tomcat6.conf in CATALINA_HOME. I add export my.var=foo to the end of file and System.getenv("my.var") now returns the value...

噩梦……

相关文章