1

I'm trying to learn how to program in java for the first time and I'm getting this error when I try to run the code in the cmd:

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 56.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)

It works in the IDE I'm using (intelliJ).

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216

2 Answers2

3

Your answer is in this line:

Exception in thread "main" java.lang.UnsupportedClassVersionError: 
Main has been compiled by a more recent version of the Java Runtime 
(class file version 56.0), this version of the Java Runtime only 
recognizes class file versions up to 52.0

You have a version mismatch. You have compiled your code in IntelliJ using Java 12, but you are using Java 8 in your cmd (you can confirm this using java -version command).

The solution is to set your cmd to Java 12 (this can be done by changing the JAVA_HOME and PATH environment variables in Windows).

FYI this answer explains the class file version numbers you are seeing in the error message.

vs97
  • 5,765
  • 3
  • 28
  • 41
0

What's happening here is that you have an older version of Java in your system than the one IntelliJ is compiling for. It works in IntelliJ because it has its own Java executable inside.

If you run java -version in the command line, it will output the version you have in the system; you can select that as the compilation target in your IDE so it will work properly when you run it form cmd.

iajrz
  • 749
  • 7
  • 16