Hi, This is a common problem when you want to run a java program using the command line. The problem is occurring for so many reasons. Most of the cause is the different versions of the Java JDK and the Java JRE versions.
I have been having this problem for a while, but now I have figured it out.
It turns out that Java JDK 15 have both the JRE and the JDK inside the bin folder of the Java 15 JDK. (I have always believed that JRE and JDK are different application, but it turns out things have changed)
My problem was that I had Java JDK 15 installed on my computer
At the same time, I had Java 8 (JRE) installed on my computer.
So my computer is getting confused.
The error looks like:
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 59.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)
Open the command prompt to check the version of the java and java compiler:
Typed the following commands:
// this gave me Java 8
After that, type
// this gave me Java 15
In other words, my program is getting compiled with Java 15 and I am trying to run with Java 8. This is the problem of ” a JNI error has occurred please check your installation and try again Exception in thread main ……”
To solve the problem, I uninstalled Java 8 JRE from my computer.
Went back to the command prompt to check if “java -version” and “javac -version” are returning the same version number or not, In my case it’s yes, it is returning Java 15.
Tried recompiling my program, and running it. It Worked!!
Wow! It works fine. If not then comment……