Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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

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:

introducir la descripción de la imagen aquí

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 ?

like image 765
Revolucion for Monica Avatar asked Apr 06 '19 15:04

Revolucion for Monica


People also ask

How do I find the Java Runtime class version?

In Java, we can use javap -verbose className to print out the Java class file information; the output includes the major version number. Or we can use the javap -verbose JavaClassName | grep major to display only the major version of the Java class file.

How do I fix Java Lang UnSupportedClassVersionError in Java?

UnsupportedClassVersionError happens because of a higher JDK during compile time and lower JDK during runtime. How can i make above change? Project -> Properties -> Java Compiler Enable project specific settings. Then select Compiler Compliance Level to 1.7, 1.6 or 1.5, build and test your app.


1 Answers

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.

like image 161
Jorn Vernee Avatar answered Sep 19 '22 15:09

Jorn Vernee