TOMCAT_OPTS, environment variable and System.getEnv()

匿名 (未验证) 提交于 2019-12-03 01:29:01

问题:

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

回答1:

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.



回答2:

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



回答3:

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" 


回答4:

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



回答5:

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



回答6:

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 


回答7:

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.



回答8:

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)


回答9:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!