Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
305 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...