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

时间:2023-01-28 00:22:27

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 ! But I need to launch tomcat like above in production mode.

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

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

编辑:当直接使用导出MY_VAR时,它在本地运行但不在我的服务器上运行...

9 个解决方案

#1


17  

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. Use System.getProperty to get the value of foo.

使用-Dfoo = bar调用Java时,不设置环境变量:传递系统属性。使用System.getProperty获取foo的值。

#2


15  

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...

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

Nightmare...

恶梦...

#3


13  

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"

#4


4  

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

#5


3  

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 :)

我知道这是一个老问题,但也许对其他人有用:)

#6


3  

For Tomcat7 + Ubuntu:

Set:

组:

  1. Open /etc/default/tomcat7 file
  2. 打开/ etc / default / tomcat7文件
  3. Add line somekey=value

    添加一行somekey = value

    Note: variable's name can't contain dots.

    注意:变量的名称不能包含点。

  4. Restart Tomcat: service tomcat7 restart
  5. 重启Tomcat:service tomcat7 restart

Read:

读:

System.getenv("somekey");

System.getenv( “somekey”);

#7


1  

If you want to set environment variable in tomcat to get in through getEnv, use setenv.

如果要在tomcat中设置环境变量以通过getEnv进入,请使用setenv。

I.e. in tomcat/bin you have (or should create) setenv.sh (or setenv.bat for шindoшs) and define

即在tomcat / bin中你有(或者应该创建)setenv.sh(或者用于шindoшs的setenv.bat)并定义

my.var=FOO
OTHERVAR=BOO

prepending it with set for шindoшs.

在它前面设置шindoшs。

#8


0  

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.

既然您已经向我解释过您正在使用基于yum的安装(建议使用Red Hat发行版衍生产品),如果您将Tomcat作为守护程序运行,那么您需要设置“export TOMCAT_OPTS = ...”命令在/ etc / profile(对于全局作用域)中,或将其添加到启动Tomcat实例的用户的home中的〜/ .profile或〜/ .bashrc文件中。

#9


0  

Are you using Tomcat on Eclipse IDE? Then you just need follow this steps:

您是否在Eclipse IDE上使用Tomcat?然后你只需要按照以下步骤操作:

  1. Double click on tomcat server
  2. 双击tomcat服务器
  3. Open launch configuration
  4. 打开启动配置
  5. Environment
  6. 环境
  7. New (Name / Value)
  8. 新(名称/价值)

#1


17  

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. Use System.getProperty to get the value of foo.

使用-Dfoo = bar调用Java时,不设置环境变量:传递系统属性。使用System.getProperty获取foo的值。

#2


15  

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...

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

Nightmare...

恶梦...

#3


13  

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"

#4


4  

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

#5


3  

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 :)

我知道这是一个老问题,但也许对其他人有用:)

#6


3  

For Tomcat7 + Ubuntu:

Set:

组:

  1. Open /etc/default/tomcat7 file
  2. 打开/ etc / default / tomcat7文件
  3. Add line somekey=value

    添加一行somekey = value

    Note: variable's name can't contain dots.

    注意:变量的名称不能包含点。

  4. Restart Tomcat: service tomcat7 restart
  5. 重启Tomcat:service tomcat7 restart

Read:

读:

System.getenv("somekey");

System.getenv( “somekey”);

#7


1  

If you want to set environment variable in tomcat to get in through getEnv, use setenv.

如果要在tomcat中设置环境变量以通过getEnv进入,请使用setenv。

I.e. in tomcat/bin you have (or should create) setenv.sh (or setenv.bat for шindoшs) and define

即在tomcat / bin中你有(或者应该创建)setenv.sh(或者用于шindoшs的setenv.bat)并定义

my.var=FOO
OTHERVAR=BOO

prepending it with set for шindoшs.

在它前面设置шindoшs。

#8


0  

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.

既然您已经向我解释过您正在使用基于yum的安装(建议使用Red Hat发行版衍生产品),如果您将Tomcat作为守护程序运行,那么您需要设置“export TOMCAT_OPTS = ...”命令在/ etc / profile(对于全局作用域)中,或将其添加到启动Tomcat实例的用户的home中的〜/ .profile或〜/ .bashrc文件中。

#9


0  

Are you using Tomcat on Eclipse IDE? Then you just need follow this steps:

您是否在Eclipse IDE上使用Tomcat?然后你只需要按照以下步骤操作:

  1. Double click on tomcat server
  2. 双击tomcat服务器
  3. Open launch configuration
  4. 打开启动配置
  5. Environment
  6. 环境
  7. New (Name / Value)
  8. 新(名称/价值)