简体   繁体   English

如何在 tomcat 中设置自定义环境变量?

[英]How to set custom environment variables in tomcat?

I have few key- value pair variables in my program which is hard coded now.我的程序中的键值对变量很少,现在是硬编码的。

String pswd = StringUtils.defaultString(System.getProperty("KEY_STORE_PASSWORD"), "password");
String algorithm = StringUtils.defaultString(System.getProperty("KEY_STORE_ALGORITHM"), "SunX509");

I need to load those values dynamically.我需要动态加载这些值。 For that purpose, I need to set those values as environment variables(custom) in Tomcat.为此,我需要在 Tomcat 中将这些值设置为环境变量(自定义)。 I am running the application using the Tomcat plugin.我正在使用 Tomcat 插件运行应用程序。 I tried with setenv.bat file concept.我尝试使用 setenv.bat 文件概念。 I added the following line into it.我在其中添加了以下行。

set KEY_STORE_PASSWORD=password

but it I am not getting it my logs.但我没有得到它我的日志。 I used another method我用了另一种方法

set JAVA_OPTS=-DKEY_STORE_PASSWORD=password

I added this line, tried, yet nothing...except null get printed in the console.我添加了这条线,试过了,但什么都没有……除了 null 在控制台中打印。 I don't know what is JAVA_OPTS, I didn't add any System environment variables for Tomcat.我不知道什么是JAVA_OPTS,我没有为Tomcat添加任何系统环境变量。 Should I add them first??我应该先添加它们吗? What are those variables we need to add as environment variables for Tomcat??我们需要添加哪些变量作为 Tomcat 的环境变量? Is JAVA_OPTS one of them?? JAVA_OPTS 是其中之一吗?

Can I create custom environment variables without creating them??我可以创建自定义环境变量而不创建它们吗?

 String pswd1=System.getProperty("KEY_STORE_PASSWORD");

    logger.info("pswd1 from tomcat"+ pswd1);

These are the printing statements I am using.这些是我正在使用的打印语句。

This was definitely a rough one, because all of the approved answers here in the stack over flow failed in my case.这绝对是一个粗略的问题,因为在我的情况下,堆栈溢出中所有已批准的答案都失败了。 I tried with both Setenv.bat & Catalina.bat files I even tried creating a configuration file in the CATALINA_HOME/conf folder of the tomcat, called variables.conf declaring all the variables, both key and values,I need to access as an environment variable.我尝试使用Setenv.bat & Catalina.bat files我什至尝试在 tomcat 的CATALINA_HOME/conf文件夹中创建一个配置文件,称为variables.conf声明所有变量,包括键和值,我需要作为environment variable.访问environment variable.

Pass user defined environment variable to tomcat 将用户定义的环境变量传递给 tomcat

All of these methods in the above link failed and finally I tried this one using JVM Settings of the Tomcat上述链接中的所有这些方法都失败了,最后我使用JVM Settings of the Tomcat 尝试了这个

  1. Open Window -->> Preferences -->> Tomcat -->> JVM Settings打开 Window -->> 首选项 -->> Tomcat -->> JVM 设置

    Here in the Append to JVM Parameters , add your variables which you need to work as environment variables(which can be accessed in the entire project using System.getProperty() ).Append to JVM Parameters中,添加您需要作为环境变量工作的变量(可以使用System.getProperty()在整个项目中访问)。

An example is shown in the picture below:一个例子如下图所示:

在此处输入图像描述

Here my variables are:这里我的变量是:

KEY_STORE_PASSWORD=
KEY_STORE_ALGORITHM=
KEY_STORE=
KEY_STORE_PROTOCOL=

which can be written to JVM Settings as -D followed by variable [equals to] variable value.可以将其写入 JVM 设置为-D followed by variable [equals to] variable value.

Eg: -DKEY_STORE_PASSWORD=password例如: -DKEY_STORE_PASSWORD=password

If you have any queries, please do ask.如果您有任何疑问,请务必询问。

@Bruce Wayne @布鲁斯·韦恩

Thanks for this answer!感谢您的回答! I could not find an example of how to do this anywhere!我在任何地方都找不到如何执行此操作的示例!

ADDITIONAL HELPFUL INFO for debugging with VSCode and the Tomcat for Java Extension使用 VSCode 和 Tomcat 进行调试的附加帮助信息,用于 Java 扩展

When debugging with Tomcat by right clicking on the tools folder and selecting Debug with Tomcat Server launch.json is NOT used.使用 Tomcat 进行调试时,通过右键单击工具文件夹并选择使用 Tomcat 服务器启动进行调试。不使用 json。 So trying to set the environment variables there does not work.所以试图在那里设置环境变量是行不通的。

Right click on the Tomcat server and select "Customize JVM Options"右键单击 Tomcat 服务器和 select “自定义 JVM 选项”

Then set your variables as indicated above with the -D option in the jvm.options file that appears and save然后使用出现的 jvm.options 文件中的 -D 选项设置变量,并保存

so所以

-DVARIABLENAME=VALUE -DVARIABLENAME=值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM