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

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...