javatomcatintellij-ideatomcat7unsupported-class-version

tomcat UnsupportedClassVersionError


I have a bitnami Tomcat 7 installation (apache + tomcat + mysql) on my Mac, with uses java 1.7 76. My Mac uses the java 1.8 runtime. When I deploy my project (made in intellij on the same Mac) to tomcat and try to run it I get a UnsupportedClassVersionError. It does not matter which version of the JDK I use to compile the project (I've tried the apple 1.6, oracle 1.7 and oracle 1.8 versions) and it also does not seem te matter what language level I set. The only way the get the app running is using language level 1.3 (using JDK 1.7 of 1.8)

To fix the problem I have now set tomcat to run on the 1.8 JDK installed on my Mac, I then configured intellij to use the same JDK. Now everything works fine.

My question: Do I always need to compile using the exact same version of the JDK as the java version that runs tomcat? Or am I doing something wrong.

The complete error: (sometimes also shows UnsupportedClassVersionError: minor version)

HTTP Status 500 - javax.servlet.ServletException: java.lang.UnsupportedClassVersionError: test/TestClass

type Exception report

message javax.servlet.ServletException: java.lang.UnsupportedClassVersionError: test/TestClass

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

exception

org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.UnsupportedClassVersionError: test/TestClass
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

javax.servlet.ServletException: java.lang.UnsupportedClassVersionError: test/TestClass
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:916)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:845)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:94)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.UnsupportedClassVersionError: test/TestClass
    org.apache.jsp.index_jsp._jspService(index_jsp.java:74)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.61 logs.

Solution

  • It is always a good practice to use one version of JDK and JRE for compile time and runtime phases, whether used from IDE or Tomcat. Also make sure to use same version of JRE that you used for running your test cases (if any) & tomcat to avoid running into these kind of issues.

    My question: Do I always need to compile using the exact same version of the JDK as the java version that runs tomcat?

    Sometime you might get away with this issue if you compile your code on a lower version and run it on a higher version but you should avoid this situation if possible. So try to use same JDK version from IDE for compilation and same version of JRE from Tomcat for deploying your project.