Mitrakov Artem
Mitrakov Artem

Reputation: 1513

Tomcat 7 with Java 8 on Windows and Linux

I got the following trouble:

I wanna use Java Servlet built on target Java-8 with Apache Tomcat 7.0.54.

When I run it on my local machine (Win-64, jdk build 1.8.0-b132) it works perfectly.

But when I deploy it on production (Debian GNU/Linux 7.5 (wheezy), jdk build 1.8.0-b132, Tomcat 7.0.54-2) it throws:

Unsupported major.minor version 52.0 (unable to load class ru.tomtrix.fvds.servlets.ItemServlet)

... that means unsupported Java-8 classes. So what's going wrong to Windows and Linux instances of the same version of Tomcat?

Upvotes: 11

Views: 30943

Answers (3)

Alex
Alex

Reputation: 1192

Each Tomcat instance uses by default the default JDK on the machine. You either change the default JDK (if you use one Tomcat instance), or tell Tomcat to use a different JDK.

  1. For setting a different JDK for each TOMCAT instance:

catalina.sh calls setenv.sh for using a JDK. Create file setenv.sh in CATALINA_BASE/bin, if not exists. CATALINA_BASE stands for the Tomcat folder. Open setenv.sh with gedit:

sudo gedit setenv.sh

write this line:

export JAVA_HOME=/path/to/your/JDK

Tomcat recommends exporting JAVA_HOME in setenv.sh, not in catalina.sh.

  1. For changing the default JDK on Ubuntu:

    sudo update-alternatives --install /your/path/to/JDK/bin/java

or:

sudo update-alternative --install  ${JAVA_HOME}/bin/java

if you have JAVA_HOME defined.

Upvotes: 3

Mitrakov Artem
Mitrakov Artem

Reputation: 1513

Thank you, everyone, I found what happened. It turned out that JAVA_HOME was not defined in a proper way (as logoff and Marko Topolnik mentioned). I opened /etc/default/tomcat7 and changed this section:

# The home directory of the Java development kit (JDK). You need at least
# JDK version 6. If JAVA_HOME is not set, some common directories for
# OpenJDK, the Oracle JDK, and various Java SE 6+ versions are tried.
#JAVA_HOME=/usr/lib/jvm/openjdk-6-jdk
JAVA_HOME=/path/to/my/jdk

... and everything starts to work! Does Tomcat really use an internal JDK smth like OpenJDK by default?

Upvotes: 13

Mind Peace
Mind Peace

Reputation: 915

The JAVA_HOME environment variable is irrelevant to how Eclipse will run code. Likewise the compiler settings you've shown for a project don't affect how code is run.

Instead, you need to look at the Run Configuration you are using and check the environment there. Make sure you're using Java 8, and all should be well. Click on the triangle next to the Run button, and select "Run Configurations..." to open the dialog with all the settings. Then look at the JRE tab, and ensure you're using the right JRE.

Upvotes: 0

Related Questions