3

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.
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Does this answer your question? [How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version](https://stackoverflow.com/questions/10382929/how-to-fix-java-lang-unsupportedclassversionerror-unsupported-major-minor-versi) – 9ilsdx 9rvj 0lo Jun 07 '21 at 11:16

1 Answers1

1

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.

justAbit
  • 4,226
  • 2
  • 19
  • 34
  • Good to know that my current solution is the right one. Thank you for your explanation. – projectIncomplete May 15 '15 at 11:18
  • If that is the case you can mark it resolved then :) – justAbit May 15 '15 at 11:31
  • 1
    Some additional pointers: Note this also applies to all libraries used: Have a jar in there built on Java8 and run it on Java7 might pose the same problem - not only your custom built code. Also, if your tomcat used to run on Java8 and then you change it to Java7, it will have compiled the JSP files with Java8 already. – Olaf Kock May 15 '15 at 12:16
  • @OlafKock Thank you for the additional information. – projectIncomplete May 15 '15 at 12:59