[英]TOMCAT_OPTS, environment variable and System.getEnv()
I use tomcat and I want to get an environment variable in my java code. 我使用tomcat,我想在我的java代码中获取一个环境变量。
To set an environment variable, I use this bash command : 要设置环境变量,我使用此bash命令:
export TOMCAT_OPTS=-Dmy.var=foo
After it I start tomcat 之后我开始tomcat
./startup.sh (in bin folder of tomcat)
In my java code, I try to get this variable : 在我的java代码中,我尝试获取此变量:
System.getEnv("my.var")
But it returns NULL. 但它返回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 ! 我确切地说,如果我使用maven启动tomcat并使用eclipse环境选项卡,则找到该变量! But I need to launch tomcat like above in production mode.
但我需要在生产模式下像上面一样启动tomcat。
EDIT: when using export MY_VAR directly it runs in local but not on my server... 编辑:当直接使用导出MY_VAR时,它在本地运行但不在我的服务器上运行...
System.getEnv
returns environment variables like PATH or, in your example, TOMCAT_OPTS). System.getEnv
返回环境变量,如PATH,或者在您的示例中,返回TOMCAT_OPTS)。
When you invoke Java with -Dfoo=bar
, you don't set an environment variable : you pass a system property. 使用
-Dfoo=bar
调用Java时,不设置环境变量:传递系统属性。 Use System.getProperty
to get the value of foo. 使用
System.getProperty
获取foo的值。
I finally found a config file named tomcat6.conf in CATALINA_HOME. 我终于在CATALINA_HOME中找到了一个名为tomcat6.conf的配置文件。 I add export my.var=foo to the end of file and System.getenv("my.var") now returns the value...
我将导出my.var = foo添加到文件末尾,System.getenv(“my.var”)现在返回值...
Nightmare... 恶梦...
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. 如果您使用的是tomcat7和unbuntu操作系统,则可以编辑/ etc / default / tomcat7文件,只需添加一行yourvar = yourvalue就可以了。
like below: 如下:
# Run Tomcat as this user ID. Not setting this or leaving it blank will use the
# default of tomcat7.
TOMCAT7_USER=tomcat7
# Run Tomcat as this group ID. Not setting this or leaving it blank will use
# the default of tomcat7.
TOMCAT7_GROUP=tomcat7
IM4JAVA_TOOLPATH=/usr/local/bin/
# The home directory of the Java development kit (JDK). You need at least
# JDK version 1.5. If JAVA_HOME is not set, some common directories for
# OpenJDK, the Sun JDK, and various J2SE 1.5 versions are tried.
#JAVA_HOME=/usr/lib/jvm/openjdk-6-jdk
# You may pass JVM startup parameters to Java here. If unset, the default
# options will be: -Djava.awt.headless=true -Xmx128m -XX:+UseConcMarkSweepGC
#
# Use "-XX:+UseConcMarkSweepGC" to enable the CMS garbage collector (improved
# response time). If you use that option and you run Tomcat on a machine with
# exactly one CPU chip that contains one or two cores, you should also add
# the "-XX:+CMSIncrementalMode" option.
JAVA_OPTS="-Djava.awt.headless=true -Xmx2048m -XX:+UseConcMarkSweepGC"
in Tomcat8 installed by just unpacking the archive, there is a file called " catalina.properties " 在Tomcat8中通过解压缩归档安装,有一个名为“ catalina.properties ”的文件
You can introduce environment variables in this file by just adding 您只需添加即可在此文件中引入环境变量
my.special.variable=some_value
Set: 组:
/etc/default/tomcat7
file /etc/default/tomcat7
文件 Add line somekey=value
添加一行
somekey=value
Note: variable's name can't contain dots. 注意:变量的名称不能包含点。
service tomcat7 restart
service tomcat7 restart
Read: 读:
System.getenv("somekey");
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: 有一个tomcat的配置文件,默认情况下位于
/dev/tomcat6/tomcat6.conf
我相信(查看/etc/init.d/tomcat以查看“TOMCAT_CFG”的值是什么。这是“sourced” .
这个文件( . $TOMCAT_CFG
)在tomcat启动(或停止,重启等)之前,所以如果你添加这行:
MY_VAR=somevalue
That should be available to your java application. 这应该可用于您的Java应用程序。
I know this is an old question, but maybe it will be useful to someone else :) 我知道这是一个老问题,但也许对其他人有用:)
If you want to set environment variable in tomcat to get in through getEnv , use setenv . 如果要在tomcat中设置环境变量以通过getEnv进入 ,请使用setenv 。
Ie in tomcat/bin you have (or should create) setenv.sh
(or setenv.bat
for шindoшs) and define 即tomcat的/ bin中你有(或应该创建)
setenv.sh
(或setenv.bat
为шindoшs)和定义
my.var=FOO
OTHERVAR=BOO
prepending it with set
for шindoшs. 在它前面
set
шindoшs。
Are you using Tomcat on Eclipse IDE? 您是否在Eclipse IDE上使用Tomcat? Then you just need follow this steps:
然后你只需要按照以下步骤操作:
既然您已经向我解释过您正在使用基于yum的安装(建议使用Red Hat发行版),如果您将Tomcat作为守护程序运行,那么您需要设置“export TOMCAT_OPTS = ...”命令在/ etc / profile(对于全局范围)中,或将其添加到启动Tomcat实例的用户的home中的〜/ .profile或〜/ .bashrc文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.