Tomcat 7 Throwing Unsupported Class Version Error

13,471

Solution 1

UnsupportedClassVersionError always appears when you compile "with" one version (targeting one version) and execute with a previous java version.

JAVA_HOME does not tell the operating system where should it look for the java.exe file. The operating system will take that file from the PATH variable. That's why it's recommended to add at the beginning of the PATH variable, the path JAVA_HOME/bin.

If it is not the case, the stacktrace (the rest of the error message) could be useful. In particular the "Unsupported major.minor" or something like that, and the numbers.

Additional information.

Solution 2

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.

Share:
13,471

Related videos on Youtube

Doug
Author by

Doug

Updated on September 18, 2022

Comments

  • Doug
    Doug over 1 year

    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

  • Doug
    Doug over 10 years
    The compiler compliance level is set to 1.7. I haven't done anything involving Java 8.
  • Doug
    Doug over 10 years
    I changed the path to C:\Program Files\Java\jdk1.7.0_25\bin and moved that location to the beginning of the variable (It was set to just C:\Program Files\Java\jdk1.7.0_25 prior). Unfortunately, I've got no change. I also edited the stack trace into the question. Not sure how I managed to forget that.
  • Gonzalo
    Gonzalo over 10 years
    OK. It says that, for whatever reason, you are not executing with Java 7. I understand you would have restarted after the change in PATH. Could you please type java -version in the command line?
  • Doug
    Doug over 10 years
    I restarted Tomcat after changing the path, and when that didn't work I restarted the computer, still nothing though. java -version gives me: java version "1.7.0_25" Java(TM) SE Runtime Environment (build 1.7.0_25-b17) Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode) It looks like it's using the right version.
  • Gonzalo
    Gonzalo over 10 years
    Yeah. Do you have set JRE_HOME? Apparently when JRE_HOME and JAVA_HOME are both set, Tomcat uses JRE_HOME.
  • Doug
    Doug over 10 years
    I do not have JRE_HOME set as anything. It's not in the list, so I don't think it is interfering unless I need it for something.
  • Doug
    Doug over 10 years
    I am coding it in Eclipse (where it runs fine), then exporting it to a WAR file and copying it to Tomcat's webapps directory where it gets deployed. I can see it unpack the WAR file, so I know that much is working.
  • Gonzalo
    Gonzalo over 10 years
    OK. Are you running Tomcat with your user, or as a service?. If you are running it as a service, maybe the user it's running as has a different configuration (JAVA_HOME and PATH). Are those variables system or user variables.
  • Jukka
    Jukka over 10 years
    Also make sure project facets (right-click on project => properties => Project facets) has Java 1.7.
  • Doug
    Doug over 10 years
    I am running Tomcat as a service, however both JAVA_HOME and PATH are system variables, not user variables, so Tomcat should still be using them right?
  • Doug
    Doug over 10 years
    In project facets the three things checked are Dynamic Web Module - 3.0, Java - 1.7, and JavaScript - 1.0. That seems to be good, I dunno if the other two versions have anything to do with this.