Why do I get LinkageError when only one version of Java seems to be available?

Chris Anderson picture Chris Anderson · Oct 31, 2018 · Viewed 16k times · Source

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?

Answer

Chris Anderson picture Chris Anderson · Oct 31, 2018

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.