Issue
I'm just starting to use Java. I've been following the instructions in the answer to this question in order to compile and run my first Java program. I have just installed JDK 11 and have set the path to C:\Program Files\Java\jdk-11\bin
. Having done that, I've been able to compile my program using the following command:
javac HelloWorld.java
(The file is called HelloWorld.java
.)
As a result of this a file called HelloWorld.class
appears in the directory. Now I've tried to run the program using this command:
java HelloWorld
This doesn't work. I get the following output:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: HelloWorld 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)
I have looked at this question, which combined with the error message I'm getting suggests to me that whatever Java is running my program is meant for older-version files than my compiler is churning out.
Following the advice of the answer to the above question, I have tried running javac -target 8 HelloWorld.java
. However, this doesn't work either. I get the following warning:
warning: target release 8 conflicts with default source release 11
and no HelloWorld.class
gets created.
I have a feeling I need to have Java Runtime Environment 11, but I don't know how you get it. Searching queries like "get JRE 11" sends me back to the main Java download page, which is offering "version 8".
Appendix: the directory I'm keeping HelloWorld.java
in is called java
, if that makes any difference.
Solution
You seems to have a Public JRE 8 installed on your computer. Installer of a Public JRE copies its java.exe
and javaw.exe
into C:\Windows\System32
and do some other changes in your Windows, like changes in your registry. This C:\Windows\System32
directory is registered in the system part of the PATH
environment variable by default. In Windows you have system and user definitions of the PATH
environment variable and the system part appears first in the final PATH
environment variable. You can check the final PATH
by running the path
command in the cmd
window. So when you run java
the first java.exe
Windows find and actually run is the one in the C:\Windows\System32
directory and this java.exe
is of Public JRE 8 that can't run classes of newer Java.
Unlike previous JDK releases JDK 11 doesn't have Public JRE, so after you installed JDK 11 your Public JRE 8 wasn't upgraded. Just uninstall all your Public JREs and this will resolve your issue. You may leave previous JDKs installed, they won't disturb you.
Answered By - Rostislav Krasny
Answer Checked By - Candace Johnson (JavaFixing Volunteer)