10

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?

4 Answers 4

9

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)
Sign up to request clarification or add additional context in comments.

Comments

1

This is how you can do it

  1. sudo su and cd to /var/lib/tomcat8/bin/ (or whichever is your tomcat bin path)
  2. touch setenv.sh(if it doesn't exist), if file present already do 'vi setenv.sh'
  3. chmod 777 setenv.sh (make file executable)
  4. vi setenv.sh and set following line in setenv.sh export key=value
  5. 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);
}

1 Comment

This doesn't seem to work. The environment variables don't get set in Tomcat.
0

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.

3 Comments

I don't believe it's a good idea to modify the native Apache script, /etc/init.d/tomcat6. Your changes may interfere with a future update, plus you're modifying default Tomcat behavior. Seems hacky to me.
I agree with you. Please suggest me any other way to do it. I have set all the environment on AWS CloudFormation for automation in AWS Infrastructure. So probably it will do all the things automatically. But still looking for the better suggestion.
I'm not sure if you ever got it to work using the AWS userdata, cause I'm pretty sure tomcat doesn't pick those up. Please see my answer above for an alternative.
-1

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.

4 Comments

But i cant restart the system.Is there any way to do this without restarting the system?
perhaps touch /etc/environment and source /etc/environment may do the trick.
Did not work for me, had to modify /etc/init.d/tomcat7 and add export FOO=bar
According to Ubuntu documentation when running sudo service service_name start, the environment from /etc/environment is not referenced. See also question: stackoverflow.com/questions/16645430

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.