大网数据广告活动
服务器运维问答

tomcat 8 catalina.properties add variable

· 52CE 我爱测

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…

问题原文

原帖链接 · Vasil Koicev · CC BY-SA 3.0

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.

已采纳解答

原帖链接 · BillThor · CC BY-SA 3.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-03-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。