Tomcat 7 Throwing Unsupported Class Version Error

I’m setting up a Tomcat server to host JSP sites. I have created a test application in Eclipse to make sure everything is working. All it does is print text in JSP, then call a function in a custom class which also prints text. This runs fine in Eclipse (running on the same computer as Tomcat), put when I compile the program into a WAR file and try to run it from Tomcat, it throws the exception:

message javax.servlet.ServletException: java.lang.UnsupportedClassVersionError: jsp/test/TestClass : Unsupported major.minor version 51.0 (unable to load class jsp.test.TestClass)

description The server encountered an internal error that prevented it from fulfilling this request.

org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.UnsupportedClassVersionError: jsp/test/TestClass : Unsupported major.minor version 51.0 (unable to load class jsp.test.TestClass)
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
    ...

root cause

javax.servlet.ServletException: java.lang.UnsupportedClassVersionError: jsp/test/TestClass : Unsupported major.minor version 51.0 (unable to load class jsp.test.TestClass)
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
    ...

root cause

java.lang.UnsupportedClassVersionError: jsp/test/TestClass : Unsupported major.minor version 51.0 (unable to load class jsp.test.TestClass)
    org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2899)
    ...

I should point out that once I refresh the page, the error changes to just UnsupportedClassVersionError and looses the “major.minor” stuff. Not sure what’s going on there.

Google has told me that this has to do with the runtime being an older version than the compiler however as far as I can tell, both Eclipse and Tomcat are using the most recent. They are on the same computer, so they should be using the same JAVA_HOME variable right?
I have a number of different JRE / JDK installations and I’ve tried several of them as JAVA_HOME though nothing seemed to change (I am restarting Tomcat each time). As of right now it is:

JAVA_HOME = C:\Program Files\Java\jdk1.7.0_25
Path = C:\Program Files\Java\jdk1.7.0_25\bin; ...

Is there anything else that needs to be set for Tomcat to use this version of Java? Or is there a different location I need to point JAVA_HOME to? I have tried several of the JRE installations as well but come up with nothing. Any ideas with this?

This is all being done on Windows Server 2008 R2 using Tomcat 7.

Thanks in advance.

Doug

Answer

In Eclipse, go to your Project Preferences, go to the Java Compiler settings.

What is the compiler compliance level?

If you are running this with Tomcat 7 with Java 7 then the compliance level should be set at 1.7. If it is not, then that may be your issue. Although it should be fine as long as it is <= 1.7, you wouldn’t per chance have configured eclipse for a snapshot of Java 8?

Either way, knowing this value may help illuminate the issue.

Attribution
Source : Link , Question Author : Doug , Answer Author : zdevex

Leave a Comment