TOMCAT_OPTS, environment variable and System.getEnv()
Asked Answered
M

9

15

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

Mattins answered 25/2, 2011 at 8:59 Comment(0)
M
16

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

Mattins answered 25/2, 2011 at 11:39 Comment(1)
If it means anything, thanks to your enduring that nightmare, now I and others wandering this trail won't have to :)Propose
A
20

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.

Alternant answered 25/2, 2011 at 9:4 Comment(3)
thanks, but how can I specify environment variable to Tomcat instead ?Mattins
To create an environment variable : export FOO=BAR. To read an environment variable : System.getenv("FOO"). To pass a system property to a Java application : java -Dfoo=bar my.java.App. To read a system property : System.getProperty("foo"). To pass a system property to Tomcat : export TOMCAT_OPTS=-Dfoo=bar. To read the system property in your web app : System.getProperty("foo").Alternant
Seems that on my server, the variable can not be read (return null). In local, all runs perfectly. What can it be ?Mattins
M
16

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

Mattins answered 25/2, 2011 at 11:39 Comment(1)
If it means anything, thanks to your enduring that nightmare, now I and others wandering this trail won't have to :)Propose
E
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.

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"
Evan answered 17/7, 2013 at 5:20 Comment(2)
[For Ubuntu OS] This needs to be higher. Simplest, most consistent way to set environment variables that tomcat can readPropose
@Siddharth see Nikita Bosik 's answer :) happy codingEvan
T
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:

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

Territerrible answered 20/3, 2012 at 15:20 Comment(2)
Thanks for the answer but this post has already been answered 1 year ago ! And the solution you give is already explained into the accepted answer...Mattins
Oh yeah. Good point :) I didn't notice that one as it's so short (in spite of the tick (I blame the advert above it :P))Territerrible
Q
3

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

Quacksalver answered 3/4, 2015 at 12:19 Comment(1)
this answer is more concise and readable than my answer, I like it :)Evan
S
3

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
Shipman answered 14/11, 2015 at 11:29 Comment(0)
A
1

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.

Anaplastic answered 6/4, 2015 at 9:14 Comment(0)
B
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.

Brit answered 25/2, 2011 at 10:52 Comment(3)
It's a standard installation on Amazon EC2 linux machine with "yum install tomcat6". And using CATALINA_OPTS does not run too :(Mattins
another question, do you run the bash "export" command from a script different from the one that runs tomcat?Brit
setting the export in bash_provile or bashrc does not run but I've found a solution (see my anwser). Thanks for your helpMattins
F
0

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)
Fairweather answered 14/7, 2014 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.