JNL error after reinstalling JDK multiple times

859 Views Asked by At

Hi I am new to Java I am trying to get started in it but when I try to run my hello world script I run into this error. I have pumped this error into google and come back with a 7 year old stack overflow answer talking about versions that are compatible and what not. So I tried uninstalling and going back a version but then apparently I have to sign in and provide a company name in case of billing? I am not sure but either way I can't do that. So I reinstalled all of Java. This is what I have from a java-version:

java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) Client VM (build 25.231-b11, mixed mode)

After that I deleted the file I had compiled before and tried again. It seems to compile the file no issue but then when I try to run it I still get.

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: MyClass has been compiled by a more recent version of the Java Runtime (class file version 57.0), this version of the Java Runtime only recognizes class file versions up to 52.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

If it makes a difference this is what my hello world statement looks like.

public class MyClass {
  public static void main(String args[]) {
    System.out.println("Hello World");
  }
}

I did it just how the tutorial showed so I don't think that the issue is there but still im a noob so any help is greatly appreciated.

1

There are 1 best solutions below

9
Jannik On

The problem is, that your Java runtime is version 8 and your compiler is version 13, hence the incompatibility. If you run javac -version it will tell you something like javac 13.0.1. You should check that you uninstall Java 8 and use only the runtime bundled with your JDK. Then the version incompatibility should be gone.

Alternatively you could add --release 8 to your compiler invocation. This will tell the Java 13 compiler to produce bytecode which is compatible with Java 8.

For example consider the following class:

public class Main{
    public static void main(String[] args) {
        System.out.println("Hello world!");
        System.out.println("Running on Java version " 
           + System.getProperty("java.version"));
    }
}

Then compile it without additional flags:

> jdk13/bin/javac .\Main.java

and run it on Java 13:

> jdk13/bin/java Main
Hello world!
Running on Java version 13.0.1

and on Java 8:

> jdk8/bin/java Main
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: Main has been compiled by a more
recent version of the Java Runtime (class file version 57.0), this version of the Java Runtime only
recognizes class file versions up to 52.0
...

Which crashes as expected.

Now compile it with the release flag:

> jdk13/bin/javac --release 8 .\Main.java

And test again:

> jdk8/bin/java Main
Hello world!
Running on Java version 1.8.0_192

> jdk13/bin/java Main
Hello world!
Running on Java version 13.0.1

Everything works fine.