1

I have a project built with Java 7 (project pom excerpt) :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
    </configuration>
</plugin>

It has a dependency with a module built with Java 8 (dependency pom excerpt) :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>

I've deployed and run my application on a server running with Java 7, and I get this error :

java.lang.UnsupportedClassVersionError: com/h/i/c/u/e/TradeStatus : Unsupported major.minor version 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
at java.lang.Class.getDeclaredMethods(Class.java:1808)
at ...

How can I fix this, knowing that I can't change the JRE on the server, and that I can't change the fact that the module is built with Java 8 ?

l0r3nz4cc10
  • 1,237
  • 6
  • 27
  • 50
  • 2
    Simple answer: you can't. You're not going to be able to run code developed for Java 8 on Java 7. You either need to upgrade the JRE on the server or downgrade the dependency to one that supports Java 7 or less. – DaveyDaveDave May 16 '19 at 14:41

2 Answers2

1

UnsupportedClassVersionError ... Major Minor occurs when you have a library built in higher version of java and you want to use in your application which is running in lower version. So there are two possible solutions.

  • First build the library with code changes in required version of JDK
  • Upgrade your JDK version so that both library and your application will work.
Sambit
  • 7,625
  • 7
  • 34
  • 65
0

It really depends to what extent the library uses Java8 JVM features. You can try using a retrorlanslator like Retrolambda on the library. https://github.com/luontola/retrolambda

Check discussions in Can Java 8 code be compiled to run on Java 7 JVM?

New server or JVM upgrade on the existing one is much better use of your time through.

Lesiak
  • 22,088
  • 2
  • 41
  • 65