2

I need to read an enviromental variable from Tomcat app. I tried to do:

/opt/tomcat/conf/catalina.properties

addint at the end:

my.special.variable=some_value

and reading it by

System.getenv("my.special.variable")

I get null

Note

I have several defined at OS level (linux ubuntu) but the result of getting them is tomcat root - /opt/tomcat.

For example :

with printenv I can see variable HOME (also with echo $HOME). Running code from a program that is outside of the tomcat - the result is correct - /home/user.

Exactly the same code in app deployed in tomcat return - /opt/tomcat.

2 Answers 2

6

You can define variables in setenv.sh(Linux/UNIX) or setenv.bat (Windows). Define the variables in the appropriate manner for the O/S you are running on. For Linux/UNIX you will need to export the variables. You can also define them in the environment you start Tomcat from.

You can also create an appenv.sh or appenv.bat file for application (local) changes. Read the existing setenv.sh or setenv.bat file for details.

Periods and hyphens are not valid for environment variables. Traditionally, the names are transformed by upper-casing the name and changing periods and hyphens to underscores. That would give the the name MY_SPECIAL_VARIABLE. This would be defined with a line like:

export MY_SPECIAL_VARIABLE=some.value

You can also define Java system properties by adding a string '-Dmy.special.variable=some_valueto the command line used to start Tomcat. This is done in the same file. Insetenv.sh` you would add a line like:

export JAVA_OPTS="$JAVA_OPTS -Dmy.special.variable=some_value"

You can also use this file to tune the Java configuration. If JAVA_OPTS system variable is defined in the environment used to launch Tomcat, its value will be added to the command line.

If you use a Java system property you retrieve with a call to System.getProperty. This would be coded like:

    System.getProperty("my.special.variable");
2
  • I have several defined at OS level (linux) but the result of them is tomcat root - /opt/tomcat. Mar 6, 2017 at 7:13
  • @VasilKoicev see comment on other response.
    – BillThor
    Mar 6, 2017 at 14:48
0

Environment variables aren't Tomcat properties.

AFAIK you cannot use the properties inside the actual code, just in e.g. server.xml.

If you want to use environment variables that you can retrieve with System.getenv, you'll need to define them with your operating system; that quite depends. A classic Linux example would in /etc/default/tomcat or in the systemd service.

3
  • I have several defined at OS level (linux) but the result of them is tomcat root - /opt/tomcat. Mar 6, 2017 at 7:13
  • @VasilKoicev If you are running Tomcat as a root service, the variables may be cleared when you invoke root to start the service.. Find setenv.sh under /opt/tomcat and define your properties there or in appenv.sh in the same directory. I would use appenv.sh. Use of Java system properties is more common than OS properties.
    – BillThor
    Mar 6, 2017 at 14:46
  • great it's working! Mar 6, 2017 at 17:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .