简体   繁体   中英

JNI error : A class has been compiled by a more recent version of the Java Runtime

I have a MyFirstProgram.java file that contains two classes. I just modified the Path and tried to test the file with java and javac .

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

However, it returns me:

C:\Users\antoi\Documents\Programming\Julien>java MyFirstProgram.java
Error: Could not find or load main class MyFirstProgram.java

C:\Users\antoi\Documents\Programming\Julien>javac MyFirstProgram.java
'javac' is not recognized as an internal or external command,
operable program or batch file.

Here is the Path:

图像描述简介

It only works when I call javac directly in the folder:

C:\Users\antoi\Documents\Programming\Julien>"C:\Program Files\Java\jdk-11.0.2\bin\javac" MyFirstProgram.java

C:\Users\antoi\Documents\Programming\Julien>java MyFirstProgram
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: MyFirstProgram has been compiled by a more recent version of the Java Runtime (class file version 55.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)

Yet it can't display the message java and reveals there is an UnsupportedClassVersionError . I understand from other posts it is my Java Runtime Environnment for javac that might not be in accordance to the java command. What steps to follow so that I can print the Hello World message ?

So, the problem is that you're compiling with javac from JDK 11, and then trying to run with Java 8. Since the java.exe from %JAVA_HOME%\\bin is being picked up before C:\\Program Files\\Java\\jdk-11.0.2\\bin . And, I'm guessing it can't find javac because you added C:\\Program Files\\Java\\jdk-11.0.2\\bin to the path, without restarting your shell, which means the path is not reloaded.

To resolve this you should remove the C:\\Program Files\\Java\\jdk-11.0.2\\bin entry from the path, and then update the JAVA_HOME environment variable to point to C:\\Program Files\\Java\\jdk-11.0.2 instead. Then restart your shell and it should pick up C:\\Program Files\\Java\\jdk-11.0.2\\bin expanded from %JAVA_HOME%\\bin .

The error explains that your class is compiled by a more recent version of JDK-11.0.2 and it executes by the lower version of JDK-8 .

The same problem happens with me and in my case, I download Java Uninstall tools

This tool will check for Java versions on your computer. If out-of-date versions are found, this tool will help you remove them.

Actually, whatever way I downloaded the Java update of version 8 so that problem happens but With the help of this tool I easily uninstall that update of version-8 and after that run my program as usual.

In Environment Variables I set the System Variables/path like this which is already perfect:

Add New Variable like:

Variablename: JAVA_HOME

Variablevalue: C:\\ProgramFiles\\Java\\jdk-13.0.2

Edit the path like:

%JAVA_HOME%\\bin

The error explains that your class is compiled by a more recent version of JDK-11.0.2 and it executes by the lower version of JDK-8.

In Environment Variables Edit Path listed in System Variables . move down the JavaPath to the bottom. Now restart the shell/cmd prompt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM