我需要从Tomcat应用程序读取一个环境variables。 我试图做到:
/opt/tomcat/conf/catalina.properties
最后join:
my.special.variable=some_value
并通过阅读
System.getenv("my.special.variable")
我得到空
注意
我有几个定义在操作系统级别(Linux的Ubuntu的),但得到他们的结果是tomcat的根 – / opt / tomcat。
例如 :
与printenv我可以看到variables的HOME(也与回声$ HOME)。 从tomcat之外的程序运行代码 – 结果是正确的 – / home / user。
在tomcat中部署的应用程序中的代码完全相同 – / opt / tomcat。
您可以在setenv.sh (Linux / UNIX)或setenv.bat (Windows)中定义variables。 以正确的方式为正在运行的操作系统定义variables。 对于Linux / UNIX,您将需要导出variables。 你也可以在你启动Tomcat的环境中定义它们。
您也可以为应用程序(本地)更改创buildappenv.sh或appenv.bat文件。 有关详细信息,请阅读现有的setenv.sh或setenv.bat文件。
句点和连字符对环境variables无效。 传统上,这些名称是通过名字上面的大写字母和将连字符改变成下划线来转换的。 这会给名称MY_SPECIAL_VARIABLE 。 这将被定义为一个像这样的行:
export MY_SPECIAL_VARIABLE=some.value
您还可以通过to the command line used to start Tomcat. This is done in the same file. In添加string“-Dmy.special.variable = some_value” to the command line used to start Tomcat. This is done in the same file. In定义Java系统属性to the command line used to start Tomcat. This is done in the same file. In to the command line used to start Tomcat. This is done in the same file. In to the command line used to start Tomcat. This is done in the same file. In setenv.sh中,你可以添加一行:
export JAVA_OPTS="$JAVA_OPTS -Dmy.special.variable=some_value"
您也可以使用此文件来调整Javaconfiguration。 如果在用于启动Tomcat的环境中定义JAVA_OPTS系统variables,则其值将被添加到命令行中。
如果您使用通过调用System.getProperty检索的Java系统属性。 这将被编码为:
System.getProperty("my.special.variable");
环境variables不是Tomcat属性。
AFAIK你不能使用实际的代码内的属性,只是在例如server.xml。
如果你想使用你可以用System.getenv检索的环境variables,你需要用你的操作系统来定义它们。 这相当依赖。 一个典型的Linux例子可以在/etc/default/tomcat或systemd服务中使用。