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.
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 filesI even tried creating a configuration file in theCATALINA_HOME/conffolder of the tomcat, calledvariables.confdeclaring all the variables, both key and values,I need to access as anenvironment variable.All of these methods in the above link failed and finally I tried this one using
JVM Settingsof the TomcatOpen 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 usingSystem.getProperty()).An example is shown in the picture below:
Here my variables are :
which can be written to JVM Settings as
-D followed by variable [equals to] variable value.Eg:
-DKEY_STORE_PASSWORD=passwordIf you have any queries, please do ask.