Java appears to be outdated when using java -jar command - java

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.

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:\Users\hp1>echo %JAVA_HOME%
C:\Java\jdk-13.0.2
C:\Users\hp1>echo %PATH%
C:\Windows\system32;C:\Java\jdk-13.0.2\bin
C:\Users\hp1>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:\Windows\system32;%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.

Related

java.lang.UnsupportedClassVersionError even when $JAVA_HOME is correct

main" java.lang.UnsupportedClassVersionError: org/mje/runjobj/Main has been compiled by a more recent version of the Java Runtime (class file version 58.0), this version of the Java Runtime only recognizes class file versions up to 52.0
Specs
~ % java -version
java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)
~ % javac -version
javac 1.8.0_261
~ % echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home
This website explains how to change the JRE and compiler of your project. https://www.baeldung.com/java-lang-unsupportedclassversion. For me changing the JRE and compiler to earlier versions of java fixed the problem.
JAVA_HOME isn't used by java itself, it's sometimes used by apps that use java, like maven or gradle or your IDE (but they tend to have more robust ways of finding where java hangs out).
class file version 58 is what javac14 produces.
It is impossible for a javac that reports v1.8.0 when you run javac -version, to produce this class file. If you got this class file from someone else, ask them recompile on java8 (or use -release 8 when compiling it), or download and install adoptOpenJDK 14.
If you produced this yourself, whatever process produced it was not using the javac that responded with 1.8.0 when you ran it. You'd have to explain what ran it (maven? eclipse? intellij? gradle?) for more help.

Unsupported major.minor version 51.0 Issue

I'm using Tomcat 7.0.26 in my ubuntu server, I've installed Oracle JDK and here is the output of
java -version command
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
And output of javac -version command is :
javac 1.7.0_51
I didn't specify JAVA_HOME and JRE_HOME manually and I've installed JDK from ppa:webupd8team repository.
And in my windows machine I export my web project to war file with Eclipse and compiler version of it is 1.7, I checked it in Properties of my project, in Java Compiler section, configuration is below :
Use compliance from execution environment 'JavaSE-1.7' on the Java Build Path
In Installed JREs section in my Eclipse, default one is JDK 1.7.0_45. I also checked major version of my compiled java classes with
javap -verbose MyClass | findstr "major"
command and it is 51. But I get this exception below from Tomcat in ubuntu :
java.lang.UnsupportedClassVersionError: com/maozturk/MyClass : Unsupported major.minor version 51.0
Why am I getting this exception since version of them are same?
I still don't know exactly what problem is but it solved when I upgrade Tomcat to 7.0.52.
I know that topic is about Tomcat, but I had the same problem with Glassfish, maybe it could be helpful - I've changed java version for Glassfich to java 1.8 (after install it :) )

Error: Could not find or load main class version

I have downloaded Java on mac mavericks system. But when I type on terminal to see the Java version using command:
java version
I get the following error
Could not find or load main class version
I went to oracle website and tried a check to see if Java is installed on my system,which confirmed the Java7 is installed on the system. But why can I not see version in terminal?
Try
java -version
Without the minus sign it is trying to load a program called version.
java –version
will cause the same error (copied from webpage) as "–" is an em dash
java -version is the command to check java version
Download latest Java SE from Oracle site and install it. Then reopen cmd and check java version using java -version.
Set the Java path to bin and check it's version also. Both java and javac should be on same version.
In my case:
C:\Users\darshan>java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
C:\Users\darshan>javac -version
javac 1.8.0_121

Eclipse launch error- error code 13

Eclipse returns this error message when launching:
I am pretty sure that both the java installation and Eclipse are both for my 64-bit system.
Here is my .ini file:
Your version of Eclipse looks OK, based on the filename.
To check your version of Java, run java -version in a console. On Windows 7 with 64-bit Java 6 I get:
java version "1.6.0_27"
Java(TM) SE Runtime Environment (build 1.6.0_27-b07)
Java HotSpot(TM) 64-Bit Server VM (build 20.2-b06, mixed mode)
Check that this is the version being used by Eclipse, as shown in your error code. If not, call that version explicitly, e.g.
"C:\Program Files (x86)\Java\jre7\bin\java.exe" -version
I moved the
-vm
up below the first openfile, not the second.
PIC:

how to tell which java version cgywin use?

i have cgywin and java 7 installed on window 7, also updated the system environment variable to point to the right java version, however when I run commands in cgywin, I m not sure which java it use, here is the commands and results:
$ java -version
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) Client VM (build 23.5-b02, mixed mode)
$ javac -version
javac 1.7.0_17
$ which java
/cygdrive/c/Windows/system32/java
export
declare -x JAVA_HOME="C:\\Program Files\\Java\\jdk1.7.0_17"
why all the versions are different?
Your PATH has two directories where it can find java one is under windows which picks a version installed (I imagine using the registry) and another is in your JDK. There is no javac in your Windows directory so it finds the on in your JDK.
To keep things simple I would just have one version of Java 7 JDK installed unless you really need multiple versions. I would change your path so it have the version of Java you want first, rather than near the end.

Categories