yaming yaming - 1 year ago 122
Java Question

JDK environment variable ( Ubuntu 12.04) with tomcat

In Ubuntu,i set jdk environment(JAVA_HOME,JRE_HOME), and java -version can use.
but i can start up tomcat. some info :


  • 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



environment set /etc/profile

JAVA_HOME=/opt/jvm/java/jdk1.7.0_25
JRE_HOME=/opt/jvm/java/jdk1.7.0_25/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME
export JAVA_HOME
export JRE_HOME
export PATH


logs

sunshanming@sunshanming-vm1:~$ sudo /opt/apache-tomcat-7.0.42/bin/startup.sh
[sudo] password for sunshanming:
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
sunshanming@sunshanming-vm1:~$ echo $JAVA_HOME
/opt/jvm/java/jdk1.7.0_25
sunshanming@sunshanming-vm1:~$ echo $JRE_HOME
/opt/jvm/java/jdk1.7.0_25/jre
sunshanming@sunshanming-vm1:~$ java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)

d0x d0x
Answer Source

Try to set the variables in setenv.sh in tomcats bin folder. Thats where you specify the environment for tomcat.

Just create this file in tomcat/bin/setenv.sh

#!/bin/bash
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/
export CATALINA_OUT=/var/log/tomcat/catalina.out
export CATALINA_PID=/var/log/tomcat/catalina.pid

You can also specify other env. vars for you application there.

Note: They don't create this file by default. Otherwise it would be overwritten on an update.

Update because of your comment:

The problem is that the /etc/profile is not loaded if you use sudo.

You can test it like this:

# Write a variable SMALLTEST into the profile file
sudo su -c "echo \"export SMALLTEST=Hello World\" >> /etc/profile"

# create a small script (like your startup.sh) that read the variable
echo "echo Variable is set to: \$SMALLTEST" > smalletst
chmod  +x smalletst 

# Execute the script with sudo
sudo ./smalletst 
Variable is set to:

And you see that the env. var is not set.

Update 2:

If you like to let your tomcat run as a service, you should have a look to the /opt/tomcat/bin/daemon.sh script. This you can put ti /etc/init.d/tomcat and specify your env. in the setenv.sh.