How to change Java version used by TOMCAT?
Asked Answered
P

8

64

I have Java 1.6 and Tomcat 5.5 installed on my system.

But Tomcat 5.5 accesses Java 1.5 and hence as the outcome I get the error Bad version number in .class file while executing java code with JSP.

How can I change the Tomcat version to Java 1.6?

UPDATE

I tried changing the JVM that the tomcat5w.exe is pointing to the version 1.6 and now I am out of the Bad version in .class file error. But now, I get the following error.

exception

org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:498)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause

java.lang.NullPointerException
myfirst.SearchLink.checkURL(SearchLink.java:20)
org.apache.jsp.Test_jsp._jspService(Test_jsp.java:52)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

What might be the root cause?

Pommel answered 1/11, 2010 at 19:12 Comment(3)
your exception now is completely different. It's for a new question. (but do some search before that ;) )Agronomics
I rectified the issue... Thanks a lot for your support Bozho.. Sincere ThanksPommel
Check Tomcat Tutorial: tomcat.apache.org/tomcat-8.5-doc/windows-service-howto.html , e.g. tomcat7 //US/Tomcat7 --JavaHome D:\Java8Mesial
A
51

When you open catalina.sh / catalina.bat, you can see :

Environment Variable Prequisites

JAVA_HOME Must point at your Java Development Kit installation.

So, set your environment variable JAVA_HOME to point to Java 6. Also make sure JRE_HOME is pointing to the same target, if it is set.

Update: since you are on Windows, see here for how to manage your environment variables

Agronomics answered 1/11, 2010 at 19:15 Comment(13)
I could not find both these file catalina.sh / catalina.bat in my Tomact 5.5 folderPommel
they are in /bin. But this is not the point. You have to set the environment variables. Don't touch the catalina files.Agronomics
You mean changing JVM C:\Program Files\Java\jre1.5.0_04\bin\client\jvm.dll from this?Pommel
JAVA_HOME is directing to version 1.6 only.. But when i used the code out.println("Java Version = "+ System.getProperty("java.version")); in my jsp code I got the version as 1.5.0_04 \Pommel
JRE HOME is not set.. I cannot find it in the section where I find the JAVA_HOMEPommel
How are you running tomcat? From command line? Service? Try from command-line, and check the value of JAVA_HOME (using echo %JAVA_HOME%).Agronomics
I am running tomcat on command line only... C:\Program Files\Java\jdk1.6.0_10 This results on using the echo in command line.Pommel
My actual requirement is described http://stackoverflow.com/questions/4070443/error-stating-bad-version-in-class-filehere more clearly...Pommel
and what is the output of running startup.bat ? There should be 4 lines showing different _HOME variables.Agronomics
While running the tomcat5.exe file, the first few lines contains many locations out of which two locations are poiting to C:\Program Files\Java\jdk1.6.0_10 onlyPommel
But I dont know how i got the version as 1.5.0_04 when i used out.println("Java Version = "+ System.getProperty("java.version"));Pommel
Try starting it with startup.bat. If it still does not work, look into setenv.bat, startup.bat and catalina.bat for 1.5Agronomics
@Agronomics Done with the version problem.. Kindly resolve my updated question.Pommel
S
23

You can change the JDK or JRE location using the following steps:

  1. open the terminal or cmd.
  2. go to the [tomcat-home]\bin directory.
    ex: c:\tomcat8\bin
  3. write the following command: Tomcat8W //ES//Tomcat8
  4. will open dialog, select the java tab(top pane).
  5. change the Java virtual Machine value.
  6. click OK.

note: in Apache TomEE same steps, but step (3) the command must be: TomEE //ES

Sterling answered 28/7, 2015 at 6:45 Comment(1)
For some reason I had the default Java path checked offAngular
C
21

If you use the standard scripts to launch Tomcat (i.e. you haven't installed Tomcat as a windows service), you can use the setenv.bat file, to set your JRE_HOME version.

On Windows, create the file %CATALINA_BASE%\bin\setenv.bat, with content:

set "JRE_HOME=%ProgramFiles%\Java\jre1.6.0_20"

exit /b 0

And that should be it.

You can test this using %CATALINA_BASE%\bin\configtest.bat (Disclaimer: I've only checked this with a Tomcat7 installation).

Further Reading:

Cart answered 18/7, 2013 at 5:59 Comment(0)
S
13

In Eclipse it is very easy to point Tomcat to a new JVM (in this example JRE6). My problem was I couldn't find where to do it. Here is the trick:

  1. On the ECLIPSE top menu FILE pull down tab, select NEW, -->Other
  2. ...on the New Server: Select A Wizard window, select: Server-> Server... click NEXT
  3. . on the New Server: Define a New Server window, select Apache> Tomcat 7 Server
  4. ..now click the line in blue and underlined entitled: Configure Runtime Environments
  5. on the Server Runtime Environments window,
  6. ..select Apache, expand it(click on the arrow to the left), select TOMCAT v7.0, and click EDIT.
  7. you will see a window called EDIT SERVER RUNTIME ENVIRONMENT: TOMCAT SERVER
  8. On this screen there is a pulldown labeled JREs.
  9. You should find your JRE listed like JRE1.6.0.33. If not use the Installed JRE button.
  10. Select the desired JRE. Click the FINISH button.
  11. Gracefully exit, in the Server: Server Runtime Environments window, click OK
  12. in the New Server: Define a new Server window, hit NEXT
  13. in the New Server: Add and Remove Window, select apps and install them on the server.
  14. in the New Server: Add and Remove Window, click Finish

That's all. Interesting, only steps 7-10 seem to matter, and they will change the JRE used on all servers you have previously defined to use TOMCAT v7.0. The rest of the steps are just because I can't find any other way to get to the screen except by defining a new server. Does anyone else know an easier way?

Straitjacket answered 8/2, 2013 at 2:45 Comment(1)
I have installed tomcat 6 using an .exe file [windows service] on windows 7 os. Thus I don't see the catalina.sh file in the bin directory for tomcat. Is there any other way, where I can change the jdk version mapped to such a tomcat installation?Immaterialize
S
5

There are several good answers on here but I wanted to add one since it may be helpful for users like me who have Tomcat installed as a service on a Windows machine.

Option 3 here: http://www.codejava.net/servers/tomcat/4-ways-to-change-jre-for-tomcat

Basically, open tomcatw.exe and point Tomcat to the version of the JVM you need to use then restart the service. Ensure your deployed applications still work as well.

Sacrarium answered 8/6, 2015 at 15:1 Comment(1)
FYI: tomcatw reads its properties from the registry - e.g.: HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Apache Software Foundation\Procrun 2.0\Tomcat7\Parameters\JavaBukharin
K
4

On Linux, Tomcat7 has a configuration file located at:

/etc/sysconfig/tomcat7

... which is where server specific configurations should be made. You can set the JAVA_HOME env variable here w/o needing to create a profile.d/ script.

This worked for me.

Kastroprauxel answered 1/7, 2013 at 20:57 Comment(4)
Not sure why the down-vote was made. A comment would be nice. Again this solution worked for me.Kastroprauxel
On which distribution of Linux did that work? I get ls: cannot access /etc/sysconfig/tomcat7: No such file or directoryAssertion
Amazon Web Service AMI LinuxKastroprauxel
@Assertion Check /etc/default/tomcat7 (askubuntu.com/questions/154953/specify-jdk-for-tomcat7)Grodin
I
0

For me, it got set from File > Project Structure > Project > sdk to right version and then apply and ok in Intellij from where I was running my tomcat.

Integrated answered 11/7, 2023 at 15:33 Comment(0)
C
-1

test open the termenal or cmd. go to the [tomcat-home]\bin directory. ex: c:\tomcat8\bin write the following command: Tomcat8W //ES//Tomcat8 will open dialog, select the java tap(top tap). change the Java virtual Machine value.

Chronoscope answered 29/9, 2015 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.