llch
llch

Reputation: 128

my tomcat uses wrong JAVA_HOME or JRE_HOME

I'm new to Tomcat. their is something wrong about Tomcat 7 on my ubuntu 10.10.

when I start Tomcat like this

llchli@llchli-virtual-machine:/usr/local/apache-tomcat-7.0.47/bin$ ./catalina.sh  run
Using CATALINA_BASE:   /usr/local/apache-tomcat-7.0.47
Using CATALINA_HOME:   /usr/local/apache-tomcat-7.0.47
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-7.0.47/temp
Using JRE_HOME:        /usr/local/jdk1.7.0_45
Using CLASSPATH:       /usr/local/apache-tomcat-7.0.47/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.47/bin/tomcat-juli.jar

although cannot start Tomcat this way, it echos Using JRE_Home /usr/local/jdk1.7.0_45, which is the correct JAVA_HOME or JRE_HOME on my computer.

but when I start Tomcat with sudo like this

llchli@llchli-virtual-machine:/usr/local/apache-tomcat-7.0.47/bin$ sudo ./catalina.sh  start
[sudo] password for llchli: 
Using CATALINA_BASE:   /usr/local/apache-tomcat-7.0.47
Using CATALINA_HOME:   /usr/local/apache-tomcat-7.0.47
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-7.0.47/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/apache-tomcat-7.0.47/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.47/bin/tomcat-juli.jar

it echos Using JRE_HOME: /usr,which is not the correct JAVA_HOME or JRE_HOME on my computer. But it start Tomcat correctly.

my /etc/environment is

llchli@llchli-virtual-machine:/usr/local/apache-tomcat-7.0.47/bin$ sudo cat /etc/environment 
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/jdk1.7.0_45/bin"
LANGUAGE="en"
JAVA_HOME="/usr/local/jdk1.7.0_45"
CLASSPATH="$CLASSPATH:$JAVA_HOME/lib"
CATALINA_HOME="/usr/local/apache-tomcat-7.0.47"

I'm a bit puzzled about this.And when I use daemon.sh like this, it generate errors.

llchli@llchli-virtual-machine:/usr/local/apache-tomcat-7.0.47/bin$ sudo ./daemon.sh run
Cannot find any VM in Java Home /usr
Cannot find any VM in Java Home /usr
Cannot locate JVM library file
Service exit with a return value of 1

It echos cannot find any VM in Java Home /usr. I'm so confused.

Any helps and advise will appreciate.

Thanks in advance.

Upvotes: 5

Views: 19745

Answers (4)

fometeo
fometeo

Reputation: 113

it is all said, it means adding to /etc/sudoers a line with:

Defaults env_keep += "JAVA_HOME JRE_HOME"

Upvotes: 0

JiLiang
JiLiang

Reputation: 21

Sudo resets the environment variables. When you run sudo, JRE_HOME is reset to its default (probably null) and Tomcat will assume its own default (/usr). You can run sudo with the -E switch to preserve the environment.

Are you sure, you must run the script as root? I have no idea how to set up Tomcat, but it is usually a good practice to run servers as a dedicated user instead of as root.

Upvotes: 2

Stephen C
Stephen C

Reputation: 718788

It sounds like "sudo" is not passing your JAVA_HOME and JAVA_PATH through to the child process.

When you run a command using "sudo" the environment variables are set according to what the sudoers file says. My reading of the "sudoers" manual entry is that the default is to NOT pass variables through. But according to the "sudo" manual entry, there is a command syntax variation that allows you to pass environment variables using "VAR=value" ...

I suggest that you read the manual entries for "sudo" and "sudoers" carefully, and then read your system's "/etc/sudoers" file to understand how it is behaving.


Having said that, launching services from the command line using "sudo" is not a good idea, especially if you are talking about "production" services. And you are generally better off using the service wrappers provided by your distro's packaging, rather than a non-packaged install from the "upstream" project.

Upvotes: 2

Ortwin Angermeier
Ortwin Angermeier

Reputation: 6183

As i already state in a comment, i strongly suggest you to use the package system to install software, it it's available in the repository. (plus there are ppa's for nearly everything). That way you will keep your system tidy and you won't have worry about things not working (usually).

My suggestion is to remove your current manual install and install the tomcat7 package.

apt-get install tomcat7

For further configuration, use the file /etc/default/tomcat7. To start/stop/restart use the command service tomcat7 {start|stop|restart}.

Using the package tomcat7 the tomcat instance will run as it's own user, and not as root, and that's good, since a service should not run as a user with full privileges.

I am aware that i am actually not answering your question with the environment variables. But using the suggested way, you will not face that problem to begin with.

Upvotes: 0

Related Questions