Tomcat does not recognize JAVA_HOME

84,419

Solution 1

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

#   Do not set the variables in this script. Instead put them into a script
#   setenv.sh 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 setenv.sh, in rare case when it is needed.
CLASSPATH=

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

When you starting tomcat using catalina.sh, it searching for file setenv.sh 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 setenv.sh in the folder CATALINA_BASE/bin, if it does not exist already.
  2. Add this line to setenv.sh

    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 setenv.sh 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.

Solution 2

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/startup.sh
    

    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:

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

    and then

    source /etc/environment
    

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


Update:

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:
84,419

Related videos on Youtube

isapir
Author by

isapir

Updated on September 18, 2022

Comments

  • isapir
    isapir over 1 year

    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 catalina.sh), and I get the following:

    user@ubuntu:~$ sudo /opt/tomcat7/apache-tomcat-7.0.53/bin/startup.sh
    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?

  • isapir
    isapir almost 10 years
    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.
  • jobin
    jobin almost 10 years
    @Igal: Edited my answer to solve that issue, you should first use sudo -i before you source. Thanks for pointing that out.
  • isapir
    isapir almost 10 years
    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...
  • jobin
    jobin almost 10 years
    @Igal: Just press Ctrl + d or type exit.
  • isapir
    isapir almost 10 years
    did that... but am still getting the original Neither the JAVA_HOME nor the JRE_HOME environment variable is defined :s
  • jobin
    jobin almost 10 years
    @Igal: Have modified my answer to reflect global environments file. Check if this helps, sorry for the incorrect file usage.
  • isapir
    isapir almost 10 years
    thank you for your patience... I made the changes to /etc/environment and called source /etc/environment but still getting the original error message...
  • jobin
    jobin almost 10 years
    @Igal: /etc/environement variables are also reset, you would need to use sudo su and then run the /opt/tomcat7/apache-tomcat-7.0.53/bin/startup.sh.
  • isapir
    isapir almost 10 years
    @Jobim thank you for your time and help. I really appreciate it.
  • jobin
    jobin almost 10 years
    @Igal: Oh yes, it can be, can you confirm if that works? Thanks!
  • isapir
    isapir almost 10 years
    YES! it worked :) but source /etc/environment was not enough. I had to reboot the server. thank you for your help!