'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. I am running the application using the Tomcat plugin. I tried with setenv.bat file concept. 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. I don't know what is JAVA_OPTS, I didn't add any System environment variables for Tomcat. Should I add them first?? What are those variables we need to add as environment variables for Tomcat?? Is JAVA_OPTS one of them??

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.



Solution 1:[1]

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.

Pass user defined environment variable to tomcat

All of these methods in the above link failed and finally I tried this one using JVM Settings of the Tomcat

  1. Open Window -->> Preferences -->> Tomcat -->> JVM Settings

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

An example is shown in the picture below:

enter image description here

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.

Eg: -DKEY_STORE_PASSWORD=password

If you have any queries, please do ask.

Solution 2:[2]

@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

When debugging with Tomcat by right clicking on the tools folder and selecting Debug with Tomcat Server launch.json is NOT used. So trying to set the environment variables there does not work.

Right click on the Tomcat server and select "Customize JVM Options"

Then set your variables as indicated above with the -D option in the jvm.options file that appears and save

so

-DVARIABLENAME=VALUE

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Richard Varno