简体   繁体   中英

TOMCAT_OPTS, environment variable and System.getEnv()

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

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.

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

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"

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

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");

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

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

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

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM