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?
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
You can change the JDK or JRE location using the following steps:
- open the terminal 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 tab(top pane).
- change the Java virtual Machine value.
- click OK.
note:
in Apache TomEE same steps, but step (3) the command must be: TomEE //ES
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:
- On the ECLIPSE top menu FILE pull down tab, select NEW, -->Other
- ...on the New Server: Select A Wizard window, select: Server-> Server... click NEXT
- . on the New Server: Define a New Server window, select Apache> Tomcat 7 Server
- ..now click the line in blue and underlined entitled: Configure Runtime Environments
- on the Server Runtime Environments window,
- ..select Apache, expand it(click on the arrow to the left), select TOMCAT v7.0, and click EDIT.
- you will see a window called EDIT SERVER RUNTIME ENVIRONMENT: TOMCAT SERVER
- On this screen there is a pulldown labeled JREs.
- You should find your JRE listed like JRE1.6.0.33. If not use the Installed JRE button.
- Select the desired JRE. Click the FINISH button.
- Gracefully exit, in the Server: Server Runtime Environments window, click OK
- in the New Server: Define a new Server window, hit NEXT
- in the New Server: Add and Remove Window, select apps and install them on the server.
- 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?
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:
- http://tomcat.apache.org/tomcat-5.5-doc/RUNNING.txt - Section: 'Advanced Configuration - Multiple Tomcat Instances'
- http://tomcat.apache.org/tomcat-7.0-doc/RUNNING.txt - Section: '(3.4) Using the "setenv" script (optional, recommended)'
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.
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.
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.
来源:https://stackoverflow.com/questions/4072260/how-to-change-java-version-used-by-tomcat