TOMCAT_OPTS、环境变量和 System.getEnv()
我使用 tomcat,我想在我的 java 代码中获取一个环境变量。
要设置环境变量,我使用以下 bash 命令:
export TOMCAT_OPTS=-Dmy.var=foo
在启动 tomcat 之后
./startup.sh (in bin folder of tomcat)
在我的 java 代码中,我尝试获取此变量:
System.getEnv("my.var")
但它返回 NULL。
我怎样才能做到这一点?
我精确地说,如果我使用maven启动tomcat并使用eclipse环境选项卡,就会找到该变量!但我需要像上面那样在生产模式下启动tomcat。
编辑:当直接使用导出MY_VAR时,它在本地运行,但不在我的服务器上运行...
I use tomcat and I want to get an environment variable in my java code.
To set an environment variable, I use this bash command :
export TOMCAT_OPTS=-Dmy.var=foo
After it I start tomcat
./startup.sh (in bin folder of tomcat)
In my java code, I try to get this variable :
System.getEnv("my.var")
But it returns NULL.
How can I do that ?
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.
EDIT: when using export MY_VAR directly it runs in local but not on my server...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
System.getEnv
返回环境变量,例如 PATH,或者在您的示例中为 TOMCAT_OPTS)。当您使用
-Dfoo=bar
调用 Java 时,您不会设置环境变量:您会传递系统属性。使用 System.getProperty 获取 foo 的值。System.getEnv
returns environment variables like PATH or, in your example, TOMCAT_OPTS).When you invoke Java with
-Dfoo=bar
, you don't set an environment variable : you pass a system property. UseSystem.getProperty
to get the value of foo.我终于在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...
Nightmare...
如果您使用的是 tomcat7 和 unbuntu 操作系统,您可以编辑 /etc/default/tomcat7 文件,
只需添加一行 yourvar=yourvalue 即可。
像下面这样:
if you are using tomcat7 and unbuntu os, you can edit the /etc/default/tomcat7 file,
just add a line of yourvar=yourvalue will do that.
like below:
tomcat 有一个配置文件,默认位于
/dev/tomcat6/tomcat6.conf
我相信(查看 /etc/init.d/tomcat 看看“TOMCAT_CFG”的值是什么。这是在 tomcat 启动(或停止、重新启动等)之前“源自”此文件 (.$TOMCAT_CFG
) 中的.
,因此如果添加以下行: :)
我知道这是一个老问题,但也许对其他人有用
There's a config file for tomcat, by default located at
/dev/tomcat6/tomcat6.conf
I believe (look in /etc/init.d/tomcat to see what the value of "TOMCAT_CFG" is. This is "sourced".
in this file (. $TOMCAT_CFG
) before tomcat is started (or stopped, restarted, etc), so if you add the line:That should be available to your java application.
I know this is an old question, but maybe it will be useful to someone else :)
对于 Tomcat7 + Ubuntu:
设置:
/etc/default/tomcat7
文件添加行
somekey=value
注意:变量名称不能包含点。
service tomcat7 restart
读取:
System.getenv("somekey");
For Tomcat7 + Ubuntu:
Set:
/etc/default/tomcat7
fileAdd line
somekey=value
Note: variable's name can't contain dots.
service tomcat7 restart
Read:
System.getenv("somekey");
在通过解压压缩包安装的Tomcat8中,有一个名为“catalina.properties”的文件,
您可以在该文件中引入环境变量,只需添加
in Tomcat8 installed by just unpacking the archive, there is a file called "catalina.properties"
You can introduce environment variables in this file by just adding
如果想在tomcat中设置环境变量通过getEnv进入,请使用setenv。
即在 tomcat/bin 中,您有(或应该创建)
setenv.sh
(或setenv.bat
for шindoшs)并定义在其前面添加
set
对于印度人来说。If you want to set environment variable in tomcat to get in through getEnv, use setenv.
I.e. in tomcat/bin you have (or should create)
setenv.sh
(orsetenv.bat
for шindoшs) and defineprepending it with
set
for шindoшs.现在您已经向我解释了您正在使用基于 yum 的安装(建议使用 Red Hat 发行版衍生版本),如果您将 Tomcat 作为守护进程运行,那么您需要设置“export TOMCAT_OPTS=...”命令在 /etc/profile 中(对于全局范围),或者将其添加到启动 Tomcat 实例的用户的主目录中的 ~/.profile 或 ~/.bashrc 文件中。
Now that you have explained to me that you are using yum based installation (which suggest a Red Hat distro derivatives), if you are running your Tomcat as daemon, then you'll need to set your "export TOMCAT_OPTS=..." command in you /etc/profile (for global scope), or add it in your ~/.profile or ~/.bashrc file at the home of the user who starts the Tomcat instance.
您在 Eclipse IDE 上使用 Tomcat 吗?然后你只需要按照以下步骤操作:
Are you using Tomcat on Eclipse IDE? Then you just need follow this steps: