Search code examples
javajavac

java.lang.UnsupportedClassVersionError - different versions of JDK and JRE


when I'm trying to compile my class(hello.java) - it's ok - no problems, but when I trying to run hello.class - I have this trouble my java version 1.8.0_211 my javac version 12.0.1 I have installed JDK 12 How to fix it?

my JDK path: D:\jdk-12.0.1\bin

C:\Users\Vasyl\Desktop>javac hello.java

C:\Users\Vasyl\Desktop>java hello
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: hello 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)

Solution

  • You should set your JAVA_HOME environment variable as below:

    JAVA_HOME=D:\jdk-12.0.1
    

    and then add the following part to the beginning of the value of your PATH environment variable:

    $JAVA_HOME/bin;
    

    This ensures that this JDK version would be picked up for both compile and runtime execution of your program.