javaunsupported-class-version

When I try to run my code with the "java" command in the cmd window, I get an error


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


Solution

  • 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.