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?
got the solution...what i have done is i have put the export statements in /etc/init.d/tomcat6
at top and restarted the server by command sudo /etc/init.d/tomcat6 restart
. So now my web-app running in tomcat server can access that 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
- sudo su and cd to /var/lib/tomcat8/bin/ (or whichever is your tomcat bin path)
- touch setenv.sh(if it doesn't exist), if file present already do 'vi setenv.sh'
- chmod 777 setenv.sh (make file executable)
- vi setenv.sh and set following line in setenv.sh export key=value
- sudo systemctl restart tomcat.service
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);
}
got the solution...what i have done is i have put the export statements in /etc/init.d/tomcat6
at top and restarted the server by command sudo /etc/init.d/tomcat6 restart
. So now my web-app running in tomcat server can access that variable.
/etc/init.d/tomcat6
. Your changes may interfere with a future update, plus you're modifying default Tomcat behavior. Seems hacky to me. –
Begay Doesnt this work?
Go to your environment file. sudo vi /etc/environment and Add the required variable. and save the file.
I think in the recent Ubuntu, You would have to restart your system for the changes to take effect.
/etc/init.d/tomcat7
and add export FOO=bar
–
Georgena sudo service service_name start
, the environment from /etc/environment
is not referenced. See also question: stackoverflow.com/questions/16645430 –
Georgena © 2022 - 2024 — McMap. All rights reserved.
/etc/init.d/tomcat6
. Your changes may interfere with a future update, plus you're modifying default Tomcat behavior. Seems hacky to me. – Begay