2

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.

Rostislav Krasny
  • 577
  • 3
  • 14
C. R. Yasuo
  • 187
  • 1
  • 9
  • 3
    Run `java --version` and `javac --version` to see the version of each you're using. Installing the JDK will give you the JRE as well, but you may have an earlier version earlier on your path. I suggest you look very carefully at your path. – Jon Skeet Sep 30 '18 at 16:20
  • 1
    I would start by using a full, absolute path to both javac and java. It could be that your javac is taken from java 11, and the java from an older installation. – GhostCat Sep 30 '18 at 16:20
  • Your java run time seems to be older than your java compiler which is causing this error. Try setting your latest java installation path in PATH env variable and then it should let you get rid of the error – Pushpesh Kumar Rajwanshi Sep 30 '18 at 16:20
  • 1
    I'd probably go through and systematically uninstall every JRE and JDK you have, then install **one** JDK+JRE combination (of the same version). – T.J. Crowder Sep 30 '18 at 16:20
  • @JonSkeet Works for `javac` (I have version 11 as I suspected), but with `java --version` I get `Unrecognised option: --version` followed by a couple of other fatal Java Virtual Machine-related error messages. – C. R. Yasuo Sep 30 '18 at 16:23
  • I've added `C:\Program Files\Java\jre1.8.0_181` (and the path of the JDK, which is in the question) to the path environment variable, but the same thing is still happening. And this is the newest version of JRE that I can get my hands on (and the newest version of JDK as far as I'm aware). – C. R. Yasuo Sep 30 '18 at 16:39
  • Also, apparently the JDK contains the JRE within it (https://stackoverflow.com/questions/1906445/what-is-the-difference-between-jdk-and-jre). So should I get rid of my separate JRE installation and replace it with only the JDK? – C. R. Yasuo Sep 30 '18 at 16:44
  • Just set your JDK path which should be this, C:\Program Files\Java\jdk1.8.0_181\bin (ensure to include bin) in PATH env variable and also make sure, your path doesn't contain any other older jdk/jre paths within PATH env variable before that. This will get rid of old JRE that is bugging you and then both javac and java will point to your latest jdk that you set in path and then it should work seamlessly – Pushpesh Kumar Rajwanshi Sep 30 '18 at 19:09
  • Yes, just get rid of the obsolete JRE – Holger Oct 01 '18 at 20:56

3 Answers3

3

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.

Rostislav Krasny
  • 577
  • 3
  • 14
  • If I uninstall my public JREs, will I still be able to run programs written by other people which need Java? – C. R. Yasuo Oct 02 '18 at 08:21
  • Yes, you will be able to run them using your JDK. If you run JAR-s not by scripts or manually in the command line but by clicking on them you should associate the JAR file in the Windows Explorer with the javaw.exe of your JDK. – Rostislav Krasny Oct 02 '18 at 10:03
0

This is happening because of the mismatch of the JRE version and the JDK version.

it looks like JRE 8 you have installed in your system but you are using JDK 11 . please go through this wikipedia

Older version of JAVA RUN ENGINE can not execute the newer version of a java class. to execute you always have to create the class of the same version.

here you can execute the code by just writing this command

compile using

javac HelloWorld.java --release 8

then execute

java HelloWorld

ujjal das
  • 897
  • 11
  • 15
0

You have to Setup PATH of Java Entertainment Variable. You can follow the Screenshot enter image description here

Amranur Rahman
  • 1,061
  • 17
  • 29