Issue: java.lang.UnsupportedClassVersionError: org/glassfish/jersey/servlet/ServletContainer
Solution 1
Your jersey lib doesn't support 1.6 Java. Either see if there's a jersey build that works with 1.6, or upgrade your Java.
UPDATE
Java 1.6 is supported in Jersey 2.6 and below. Jersey 2.7 and later is compiled with Java 7.
Solution 2
The problem is as you know using a version of jersey jar which is compiled using Java 7 and you are running using Java 6
.
Either downgrade your Jersey version or upgrade your Java version to 7.
Unsupported major.minor version 51.0
J2SE 8 = 52, J2SE 7 = 51, J2SE 6.0 = 50, J2SE 5.0 = 49, JDK 1.4 = 48, JDK 1.3 = 47, JDK 1.2 = 46, JDK 1.1 = 45
Solution 3
This is because you compiled it using a higher version of Java. For instance, compile Java 7 code which has a switch on Strings, then try to run that application on JRE 6 (which doesn't support switch on Strings) and this is what you get.
Re-compile your code in Java 6, or run the code using the version of Java in which you compiled it.
Kaushi
Updated on June 14, 2022Comments
-
Kaushi over 1 year
I have written a RESTful web services. Below is my maven dependencies.
<dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-server</artifactId> <version>2.17</version> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> <version>2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.5.6</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.6</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.3.3</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>jaxrs-api</artifactId> <version>3.0.12.Final</version> </dependency> <dependency> <groupId>org.jboss.spec.javax.annotation</groupId> <artifactId>jboss-annotations-api_1.1_spec</artifactId> <version>1.0.1.Final</version> </dependency> <dependency> <groupId>net.jcip</groupId> <artifactId>jcip-annotations</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-client</artifactId> <version>3.0.12.Final</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>3.0.12.Final</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency>
Below is my
web.xml
<servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.service</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey REST Service</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping>
I have pointed my JRE to jre6 and the compiler version to 1.6 in my eclipse. Rest Web Services are working perfectly.
I export as WAR and put the WAR file in webapps folder of tomcat and deploy. It is getting deployed. Issue is coming when I invoke the Web service, it is throwing the following error.
java.lang.UnsupportedClassVersionError: org/glassfish/jersey/servlet/ServletContainer : Unsupported major.minor version 51.0 (unable to load class org.glassfish.jersey.servlet.ServletContainer) org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2961) org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1210) org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1690) org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
Tomcat version is 7 and JDK is 1.6 and JRE is 6. I understand that error is coming because of version problem.
How can I fix this? Anyone pls help me with this.
-
Kayaman about 8 yearsIt's not his code that's the problem, it's Jersey that wants a > 1.6 Java version.
-
Kaushi about 8 yearsI have to use java 1.6 only. do you know which jersey JARs will work under java 1.6?????
-
Kayaman about 8 years@Kaushi Well, old ones definitely. They might have a special build for jdk6, I recommend checking out Maven central for the libraries.
-
Stultuske about 8 yearswhether it's the jersey code or not, the problem remains the same. If you need to use Java 6, which is not recommended, either you'll have to search a Jersey which supports Java 6, or find an alternative to it.
-
Paul Samsotha about 8 years@Kaushi Jersey <= 2.6
-
Kaushi about 8 years@peeskillet in the pom.xml, am using 2.17 version. But still no luck
-
Kaushi about 8 years@peeskillet ya u were rite.... 2.6 version fixed my issue. I got confused with the version numbers. thanks a lot... :) post this as an answer....