'How to fix LinkageError occurred while loading main class error? Using an other JDK runtime?

I want to start a Java ChatServer in IntelliJ (The Gradle JVM is set to Java 16.0.1 supposed to be!). After running the Class I get the following error:

Error: LinkageError occurred while loading main class edu.hm.cs.vt.chat.server.ChatServerStarter
        java.lang.UnsupportedClassVersionError: edu/hm/cs/vt/chat/server/ChatServerStarter has been compiled by a more recent version of the Java Runtime (class file version 60.0), this version of the Java Runtime only recognizes class file versions up to 55.0
    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Execution failed for task ':chat:server:ChatServerStarter.main()'. Process 'command '/Users/username/opt/anaconda3/bin/java'' finished with non-zero exit value 1

My local configurations are set as followed:

Java Version:

java -version
openjdk version "11.0.6" 2020-01-14

which java
/Users/username/opt/anaconda3/bin/java

It seems that my Java Version is used from conda.

Java Versions installed on machine:

/usr/libexec/java_home -V
Matching Java Virtual Machines (2):
16.0.1 (x86_64) "Oracle Corporation" - "OpenJDK 16.0.1" /Library/Java/JavaVirtualMachines/jdk-16.0.1.jdk/Contents/Home
1.8.241.07 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/Library/Java/JavaVirtualMachines/jdk-16.0.1.jdk/Contents/Home

JAVA_HOME path:

echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-16.0.1.jdk/Contents/Home

Why does the

'/Users/username/opt/anaconda3/bin/java'

appears in the error-message??

How I can configure it to use the 16.0.1 JDK or what I am doing wrong?



Solution 1:[1]

There are 3 ways to do so ,

First is with gradle.properties you need to add

org.gradle.java.home=/path/to/jdk/directory

Second way is one time test with terminal like this

gradle build -Dorg.gradle.java.home=/JDK_PATH

3rd way you can add the path to build.gradle

 compileJava.options.fork = true
 compileJava.options.forkOptions.executable = '/path_to_javac'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 George