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

发布于 2024-10-19 13:57:23 字数 466 浏览 5 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

我早已燃尽 2024-10-26 13:57:24

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

哭泣的笑容 2024-10-26 13:57:24

我终于在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...

素衣风尘叹 2024-10-26 13:57:24

如果您使用的是 tomcat7 和 unbuntu 操作系统,您可以编辑 /etc/default/tomcat7 文件,
只需添加一行 yourvar=yourvalue 即可。

像下面这样:

# 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"

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:

# 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"
剩余の解释 2024-10-26 13:57:24

tomcat 有一个配置文件,默认位于 /dev/tomcat6/tomcat6.conf 我相信(查看 /etc/init.d/tomcat 看看“TOMCAT_CFG”的值是什么。这是在 tomcat 启动(或停止、重新启动等)之前“源自”此文件 (.$TOMCAT_CFG) 中的 . ,因此如果添加以下行

MY_VAR=somevalue

: :)

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

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:

MY_VAR=somevalue

That should be available to your java application.

I know this is an old question, but maybe it will be useful to someone else :)

哭了丶谁疼 2024-10-26 13:57:24

对于 Tomcat7 + Ubuntu:

设置:

  1. 打开 /etc/default/tomcat7 文件
  2. 添加行 somekey=value

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

  3. 重启 Tomcat: service tomcat7 restart

读取:

System.getenv("somekey");

For Tomcat7 + Ubuntu:

Set:

  1. Open /etc/default/tomcat7 file
  2. Add line somekey=value

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

  3. Restart Tomcat: service tomcat7 restart

Read:

System.getenv("somekey");

幸福丶如此 2024-10-26 13:57:24

在通过解压压缩包安装的Tomcat8中,有一个名为“catalina.properties”的文件,

您可以在该文件中引入环境变量,只需添加

my.special.variable=some_value

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

my.special.variable=some_value
桜花祭 2024-10-26 13:57:24

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

即在 tomcat/bin 中,您有(或应该创建)setenv.sh(或 setenv.bat for шindoшs)并定义

my.var=FOO
OTHERVAR=BOO

在其前面添加 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 (or setenv.bat for шindoшs) and define

my.var=FOO
OTHERVAR=BOO

prepending it with set for шindoшs.

回眸一遍 2024-10-26 13:57:24

现在您已经向我解释了您正在使用基于 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.

燃情 2024-10-26 13:57:24

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

  1. 双击 tomcat 服务器
  2. 打开启动配置 新建
  3. 环境
  4. (名称/值)

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

  1. Double click on tomcat server
  2. Open launch configuration
  3. Environment
  4. New (Name / Value)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文