10

This is my Java environment:

~: java -version
    java version "11.0.1" 2018-10-16 LTS
    Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
    Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)

But when trying to execute a program after compiling it I get the following error:

Error: LinkageError occurred while loading main class ClassName
    java.lang.UnsupportedClassVersionError: ClassName 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 54.0

My PATH and JAVA_HOME variables both point to this version of java. I have other versions downloaded but it seems they are not registered in the system:

update-alternatives --display java
java - auto mode
  link best version is /usr/lib/jvm/java-11-openjdk-amd64/bin/java
  link currently points to /usr/lib/jvm/java-11-openjdk-amd64/bin/java
  link java is /usr/bin/java
  slave java.1.gz is /usr/share/man/man1/java.1.gz
/usr/lib/jvm/java-11-openjdk-amd64/bin/java - priority 1101
  slave java.1.gz: /usr/lib/jvm/java-11-openjdk-amd64/man/man1/java.1.gz

So how come the compilation and execution are done with different versions?

Chris Anderson
  • 151
  • 1
  • 1
  • 7
  • 1
    The error suggests you are using Java 10. Which OS (+ version) are you using? I seem to vaguely recall a Linux distribution (don't know which one) has released Java 10 as if it was Java 11 with the eye on it being the Long Term Support version, so they could update it to real Java 11 once it was released. – Mark Rotteveel Oct 31 '18 at 19:30
  • maybe specify the exact path to the javac executable and java app in case the the javac and java are different versions? – JGlass Oct 31 '18 at 20:54
  • @MarkRotteveel I'm using Ubuntu 18.04. However I downloaded Java from Oracles website. – Chris Anderson Oct 31 '18 at 22:14
  • @JGlass Yes that did it! Thanks :) – Chris Anderson Oct 31 '18 at 22:17
  • What worked for me: Right Click Project Name, Click on "Open Module Settings", Accept the experimental thing if it appears, click on the Project tab on the sidebar, change the SDK to the updated version that you want. Apply, press OK, and it should work. – Tigerrrrr Feb 17 '23 at 18:34

9 Answers9

7

I got this error in eclipse:

LinkageError occurred while loading main class com.example.demo.SpringDemo1Application
    java.lang.UnsupportedClassVersionError: com/example/demo/SpringDemo1Application has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 60.0
  1. Right Click on Project Name
  2. Select Properties
  3. select Java Compiler
  4. Click on Restore Defaults.
  5. Apply and close.

This works for me.

enter image description here

Guildenstern
  • 2,179
  • 1
  • 17
  • 39
vinay
  • 71
  • 1
  • 4
  • This worked for me but to improve it a little, if you need an specific version you can also select it from the dropdown menus "generated .class files compatibility" and "source compatibility" – BugsOverflow Jun 17 '22 at 19:10
  • What worked for me: Right Click Project Name, Click on "Open Module Settings", Accept the experimental thing if it appears, click on the Project tab on the sidebar, change the SDK to the updated version that you want. Apply, press OK, and it should work. – Tigerrrrr Feb 17 '23 at 18:33
  • Is this Answer using some particular IDE? – cellepo Apr 19 '23 at 22:01
5

The problem is that the other downloaded versions of Java are reachable via my PATH variable. So after specifying the full path to the javac and java executables when compiling and executing my program it works.

Chris Anderson
  • 151
  • 1
  • 1
  • 7
  • Glad it worked for you! With JBoss, I always hard code the path as it relies on specific java versions and our sys admins can't always be trusted to not update the java which would wreak havoc in production ;-) – JGlass Nov 01 '18 at 18:11
  • So... how do I actually "specify the full path to the javac and java executables when compiling and executing my program"? – Tigerrrrr Feb 16 '23 at 19:57
2

use with sudo command for update-alternatives --config javac and update-alternatives --config java. So, you can choose the same version of java for compiler and runtime environment.

I have also got same error after compiling my java program. I have screenshot ----> SCREENSHOT

brichins
  • 3,825
  • 2
  • 39
  • 60
Giriraj Soni
  • 23
  • 1
  • 5
0

This error also happens when you try to change the content of java .class file. bytecode verifier generates this error eg.

Error: LinkageError occurred while loading main class Hello java.lang.UnsupportedClassVersionError: Hello has been compiled by a more recent version of the Java Runtime (class file version 8251.8224), this version of the Java Runtime only recognizes class file versions up to 59.0

sanku
  • 9
  • 2
0

I also get this error on my computer when running a 17 version of java spring boot project.

/mvnw spring-boot:run

Check your local java version.

java --version
openjdk 11.0.12 2021-07-20 LTS

the problem is I'm starting the spring boot project was 17 java versions. the project has not required the version so I downgrade the java version on the spring boot project.

I updated the pom.xml

<properties>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

after that I run the spring boot project

/mvnw spring-boot:run

my program it works.

0

I also faced the same issue, all you need to do is to download the latest jdk in my case it is 19 and save it locally; Then, Right-click on Project in Eclipse(my IDE)

Build Path-- Configure Build Path

Java Build Path-- Select the Libraries tab

Delete the existing jre version from ModulePath

(Make sure it is having any other version)

Click on Add Library-- select JRE System Library-- Next

Click on Installed JREs, Add-- Standard VM-- click Next,

You will now see Add JRE dialog box,

Click on Directory of JRE Home, select the jdk 19 folder where you have downloaded it earlier.

0

I encountered the same problem, and it was due to having multiple versions of Java installed. To resolve it, I utilized the following command:

sudo update-alternatives --config java
Andreas Violaris
  • 2,465
  • 5
  • 13
  • 26
Alice
  • 1
  • 2
0

IntelliJ IDEA+Maven:

My problem was that my Maven .pom specified one Java version, that was different from the "Language Level"/SDK set in IntelliJ Project & Module Settings for my project - matching them all on the same version solved this same error for me.

Also be careful that any run configurations being used, have the correct version matching also.

cellepo
  • 4,001
  • 2
  • 38
  • 57
0

My problem was that the java version I was using in intellij was different from the java version used by the system hence the difference in class file versions. All I had to do was change the java version in intellij to correspond with the system version and build the project again and it worked.