Java appears to be outdated when using java -jar command

Tags: , ,



I have recently been writing some Java programs on my Windows computer. I have been trying to use java -jar to run compiled jars in order to see errors more clearly, but when I try to do this, I get the following error:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/company/app/GUI has been compiled by a more recent version of the Java Runtime (class file version 57.0), this version of the Java Runtime only recognizes class file versions up to 52.0

This obviously means that my java version is outdated. However, when I took a look at the Java Updater, it shows that I am running the latest version. Running java -version shows this:

java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) Client VM (build 25.261-b12, mixed mode)

So why is Command Prompt using an older version, and how do I change it? Thank you for your help.

Answer

This error clearly indicates that you try to run a .jar file built with JDK 13 (major version 57) on a JRE/JDK 8 (major version 52) which is provided in PATH setting and thus invoked when running java -jar / java -version commands.

If you have JDK 13 installed on your machine, you need to check environment variable PATH and/or JAVA_HOME:

C:Usershp1>echo %JAVA_HOME%
C:Javajdk-13.0.2

C:Usershp1>echo %PATH%
C:Windowssystem32;C:Javajdk-13.0.2bin

C:Usershp1>java -version
openjdk version "13.0.2" 2020-01-14
OpenJDK Runtime Environment (build 13.0.2+8)
OpenJDK 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)

If PATH refers JRE/JDK 8, you may create another variable and update PATH (copy non-java paths):

>set JAVA_13=**path_to_your_jdk_13**
>set PATH=C:Windowssystem32;%JAVA_13%bin

If you do not have JDK 13 on your machine, to resolve this issue you should rebuild the .jar file to make it compatible with JDK 8 providing that the code is not using any features from the newer versions.



Source: stackoverflow