Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
32
rated 0 times [  32] [ 0]  / answers: 1 / hits: 87214  / 2 Years ago, sun, september 4, 2022, 8:00:52

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?


More From » bash

 Answers
6

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.


[#25143] Sunday, September 4, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arbaour

Total Points: 346
Total Questions: 115
Total Answers: 105

Location: Oman
Member since Wed, Apr 12, 2023
1 Year ago
;