Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've installed Ubuntu 14.04 Server, extracted JDK1.8u5 and Tomcat7, and added the following to .profile (I also tried adding it to .bashrc with similar [non-] results):

export JAVA_HOME=/opt/java/jdk1.8.0_05
export PATH=$PATH:$JAVA_HOME/bin

when I run echo $JAVA_HOME I get the expected result of /opt/java/jdk1.8.0_05. I can also run java -version and get the correct response from Java. so far so good.

so now I try to startup Tomcat (tried also, and I get the following:

user@ubuntu:~$ sudo /opt/tomcat7/apache-tomcat-7.0.53/bin/
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program

but... I just tried echo $JAVA_HOME and it worked?

share|improve this question
up vote 11 down vote accepted

There is a help text in I will quote it here:

#   Do not set the variables in this script. Instead put them into a script
# in CATALINA_BASE/bin to keep your customizations separate.

#   JAVA_HOME       Must point at your Java Development Kit installation.
#                   Required to run the with the "debug" argument.

# Ensure that any user defined CLASSPATH variables are not used on startup,
# but allow them to be specified in, in rare case when it is needed.

if [ -r "$CATALINA_BASE/bin/" ]; then
  . "$CATALINA_BASE/bin/"
elif [ -r "$CATALINA_HOME/bin/" ]; then
  . "$CATALINA_HOME/bin/"

When you starting tomcat using, it searching for file and sourcing it. It is searching in CATALINA_HOME or CATALINA_BASE.

So the better way to set JAVA_HOME for the tomcat is:

  1. Create a script named in the folder CATALINA_BASE/bin, if it does not exist already.
  2. Add this line to

    export JAVA_HOME=/opt/java/jdk1.8.0_05
  3. Make it executable.

Why you should use this solution:

Setting environment variable in script is safer. Always try to set variables as locally as possible. Try do not use /etc/environment, /etc/profile and others if you really do not need Global Environment Variable. Setting JAVA_HOME in gives you ability to use different tomcats with different applications that need different version of java, but running by one user. Other user environment would not be affected by you.

share|improve this answer

Since you have set the environment variable for your own user and not for the superuser, you have two options:

  1. You will have to export the variable using -E option as follows:

    sudo -E /opt/tomcat7/apache-tomcat-7.0.53/bin/

    Note that this will export all environment variables while running the command. This is not preferred since the normal users environment is spilled out when you run the command as root. This is not desirable.

  2. Export the variable in root's .bashrc /etc/enviroment file. Open a terminal and type:

    sudo nano /etc/environment

    and enter your administrative password, and add the following lines to the end of the file:


    and then

    source /etc/environment

    or restart your machine and then retry the command you were using.


This answer provided hints as two why step 2 wouldn't work, sudo would reset the environment and provide a secure path, so all global variables are reset. A workaround would be to use

sudo su

and then execute command which uses the set environment variables.

share|improve this answer
Thanks @Jobin -- option 1 works so I upvoted your answer. I will now check option 2, which is my preferred solution, and if it works I will accept your answer. – Igal May 18 '14 at 17:57
I added the commands to /root/.bashrc with nano, as you suggested, but calling user@ubuntu:~$ source /root/.bashrc results in -bash: /root/.bashrc: Permission denied and trying it with sudo yields sudo: source: command not found. retrying the original command at this point brings me back to point 0. – Igal May 18 '14 at 18:05
@Igal: Edited my answer to solve that issue, you should first use sudo -i before you source. Thanks for pointing that out. – i08in May 18 '14 at 18:06
ok, pardon the newbie questions, but how do I exit the sudo -i once I'vet called source? without exiting I am getting the original error message... – Igal May 18 '14 at 18:08
@Igal: Just press Ctrl + d or type exit. – i08in May 18 '14 at 18:09

The solution I was looking for is in /etc/environment which, as specified at EnvironmentVariables, does not process scripts and does not expand variables, so adding the following (without export) did the trick:


Then I rebooted the system for the changes to take affect.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.