I want to add and environment variable which can access by my tomcat web-app. I have gone through this link but i want to set environment variable in root user. How to do that?
You can set your own variables at the command line per session, or make them permanent by placing them into the ~/. bashrc file, ~/. profile , or whichever startup file you use for your default shell. On the command line, enter your environment variable and its value as you did earlier when changing the PATH variable.
Environment Variables Bash scripts can also be passed with the arguments in the form of environment variables. This can be done in either of the following ways: Specifying the variable value before the script execution command. Exporting the variable and then executing the script.
The easiest way to set environment variables in Bash is to use the “export” keyword followed by the variable name, an equal sign and the value to be assigned to the environment variable.
According to the docs (http://tomcat.apache.org/tomcat-7.0-doc/RUNNING.txt) you should set all env vars in $CATALINA_HOME/bin/setenv.sh
EDIT: For completeness, I guess it's worth mentioning that even though this is the recommended way, the docs above state that:
By default the setenv script file is absent. If the script file is present both in CATALINA_BASE and in CATALINA_HOME, the one in CATALINA_BASE is preferred.
In case it is absent, you might also want to look for env vars in:
/etc/tomcat/tomcat[67].conf
(suse) or /etc/default/tomcat[67].conf
(e.g. ubuntu) or /etc/sysconfig/tomcat[67].conf
(rhel, fedora)This is how you can do it
In your java file you can use the following code to check if the variable is set
private static void printEnv() {
System.out.println("******************************Environment Vars*****************************");
Map<String, String> enviorntmentVars = System.getenv();
enviorntmentVars.entrySet().forEach(System.out::println);
System.out.println("******************************system Vars*****************************");
Properties enviorntmentProperties = System.getProperties();
enviorntmentVars.entrySet().forEach(System.out::println);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With