36

Ok so I loosely understand that 52.0 is Java 8, and that the exceptions means that some code is compiled with one version of java, and some with another. What I can't get my head around is which way around it is.

Here's the stack trace that I get:

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/openrdf/model/ValueFactory : Unsupported major.minor version 52.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    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:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2615)
    at java.lang.Class.getMethod0(Class.java:2856)
    at java.lang.Class.getMethod(Class.java:1668)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)

I'm executing my code from eclipse and it crashes before reaching main. I've followed a few posts on here saying to change my compliance level/my JRE, but I can't seem to get it to fix.

Is the error saying that the class 'ValueFactory' has been compiled with java 8, or that my code has? I've tried changing my compliance level to 1.6, 1.7, and 1.8, but neither of these fixed the issue.

Andy
  • 3,228
  • 8
  • 40
  • 65

3 Answers3

23

You don't need to change the compliance level here, or rather, you should but that's not the issue.

The code compliance ensures your code is compatible with a given Java version.

For instance, if you have a code compliance targeting Java 6, you can't use Java 7's or 8's new syntax features (e.g. the diamond, the lambdas, etc. etc.).

The actual issue here is that you are trying to compile something in a Java version that seems different from the project dependencies in the classpath.

Instead, you should check the JDK/JRE you're using to build.

In Eclipse, open the project properties and check the selected JRE in the Java build path.

If you're using custom Ant (etc.) scripts, you also want to take a look there, in case the above is not sufficient per se.

Mena
  • 47,782
  • 11
  • 87
  • 106
  • 1
    More or less. Just to clarify for anyone else. I was running JDK 1.7.0_79. I added jdk 1.8.0_81 and selected it in Window -> Preferences -> Java -> Installed JREs. The problem was in the Build path for the project in question, under 'Order and Export' JavaSE-1.7' was higher in the list than 'jdk1.8.0_91'. I'm guessing this means that the 1.7 system libraries were imported and preferred above 1.8. – Andy Jun 03 '16 at 11:01
  • @Andy glad to know it's working for you now. – Mena Jun 03 '16 at 11:03
10

Your code was compiled with Java 8.

Either compile your code with an older JDK (compliance level) or run it on a Java 8 JRE.

Hope this helps...

R. Horber
  • 480
  • 5
  • 11
7

Actually you have a code compiled targeting a higher JDK (JDK 1.8 in your case) but at runtime you are supplying a lower JRE(JRE 7 or below).

you can fix this problem by adding target parameter while compilation
e.g. if your runtime target is 1.7, you should use 1.7 or below

javac -target 1.7 *.java

if you are using eclipse, you can sent this parameter at Window -> Preferences -> Java -> Compiler -> set "Compiler compliance level" = choose your runtime jre version or lower.

rsingh25
  • 356
  • 3
  • 9