Unsupported major.minor version 51.0 Error when using 1.6 in pom and openjdk6 for one JUnit

StackOverflow https://stackoverflow.com/questions/22314453

  •  12-06-2023
  •  | 
  •  

I'm using Travis CI to build my code. I get unsupported major minor error. I know this error occurs when compiled version is older that executing version. But I set the compile version as 1.6 in my pom file and Travis CI also executing it with openjdk6. So, I don't see any issue of difference in java version. I cannot set my java path in pom because I use it on different servers, i.e, my computer, travis and heroku. So they may be different and so I don't want to set it in POM. You can see travis failure at https://travis-ci.org/santoshkt/cdetsd123. Any pointers are appreciated. Also if you see below aoltest.RegisterTest gets passed but aoltest.RegisterServiceTest fails.

Running aoltest.RegisterTest
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.03 sec
Running aoltest.RegisterServiceTest
Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.025 sec <<< FAILURE!
testProcessRegistration1(aoltest.RegisterServiceTest)  Time elapsed: 0.023 sec  <<< ERROR!
java.lang.UnsupportedClassVersionError: javax/servlet/http/HttpServletRequest : Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:643)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
    at aoltest.RegisterServiceTest.testProcessRegistration1(RegisterServiceTest.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:622)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:44)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
    at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:622)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
有帮助吗?

解决方案

This problem occurs because the 3.1 servlet-api .jar file you are depending upon was built with JDK7 and the classes it contains use a class file format that is only compatible on JDK7 or later.

For example, using the following Maven/gradle/ant+ivy/sbt/etc dependency in a project will fail if the project is compiled with JDK 6:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>

According to the Java EE 7 specification - which includes the Servlet 3.1 specification - JDK7 is required to run Java EE 7-based containers and applications. Therefore, whoever compiled and distributed the 3.1 servlet-api .jars to Maven central (the core Servlet API team?) had no reason/need to ensure JDK 6 binary compatibility.

Downgrading the above dependency to version 3.0.1 should solve your issue.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top