Search code examples
javaintellij-ideaunsupported-class-versionjava-runtime-compiler

Troubleshooting UnsupportedClassVersionError in IntelliJ IDEA


I'm going through a Java course, using Intellij IDEA. In this "Test" project, I have only two test classes as shown below (they really don't have any code). Before I get into the problem, I just need to state that all my projects run correctly, it's just that I have the following problem with running the terminal command, and I'm afraid I messed something up with my IDE:

The problem occurs when I need to get the serial version of the class BankAccount using the following command:

E:\Software Projects\Test\out\production\Test>serialver com.company.BankAccount

I get the error that is shown below, and I understand that the classes were compiled by a newer Java Runtime error, but how do I troubleshoot this in Intellij? I get the same error when running the command on any class in any project.

Additional note: I also get an error related to this one when running the Main class in terminal using java com.company.Main

What I think could be the cause of the problem: Prior to even trying "serialver" for the first time, I changed the name of a project folder (I just used "Rename" in my file explorer in Windows, and it messed everything up in Intellij (whatever project I opened, it had no SDK and no Run/Debug Configurations). So, I selected "Add SDK" on a project (or something similar, and selected a JRE 12 from the list. Then I ran "Debug Main" and the Run/Debug Configurations were set automatically.

When I go to Project Structure > Project Settings > SDKs I only have one SDK :"C:\Program Files\Java\jdk-12.0.1".

When I go to "Run/Debug Configurations" in a project, in the JRE of Application > Main I have the following to select (but as far as I understand, these are all one and the same SDK, right?):

  • Default (12 - SDK of 'Test' module)
  • 12(java version "12.0.1")
  • "C:\Program Files\Java\jdk-12.0.1"

So I don't know how I can adjust anything here? How was I able to compile these classes at a newer Java Runtime version at any point?

The problem here is that I'm not sure if something else is wrong, or if the prior adjustments I've made have messed something up.

The classes:

package com.company;

public class Main {

    public static void main(String[] args) {
    // write your code here
    }
}

package com.company;

public class BankAccount {
}

When running E:\Software Projects\Test\out\production\Test>serialver com.company.BankAccount

What I get in the terminal is the following error:

Exception in thread "main" java.lang.UnsupportedClassVersionError: com/company/BankAccount has been compiled by a more recent version of the Java Runtime (class file version 56.
0), this version of the Java Runtime only recognizes class file versions up to 55.0
        at java.base/java.lang.ClassLoader.defineClass1(Native Method)
        at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
        at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
        at java.base/java.net.URLClassLoader.defineClass(URLClassLoader.java:550)
        at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:458)
        at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:452)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:451)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        at java.base/java.lang.Class.forName0(Native Method)
        at java.base/java.lang.Class.forName(Class.java:398)
        at jdk.compiler/sun.tools.serialver.SerialVer.resolveClass(SerialVer.java:108)
        at jdk.compiler/sun.tools.serialver.SerialVer.serialSyntax(SerialVer.java:80)
        at jdk.compiler/sun.tools.serialver.SerialVer.main(SerialVer.java:188)

Error when running the Main class with E:\Software Projects\Test\out\production\Test>java com.company.Main:

Error: LinkageError occurred while loading main class com.company.Main
        java.lang.UnsupportedClassVersionError: com/company/Main has been compiled by a more recent version of the Java Runtime (class file version 56.0), this version of the Java Runtime only recognizes class file versions up to 55.0

Solution

  • Run java -version in the command line. If it's not Java 12 or later, it will explain the issue. You are targeting Java 12 version while run it on an older Java version.

    The solution would be to either adjust the target language level so that your code can run on older Java versions or to adjust your PATH environment so that java from JDK 12 install directory is the first. Or you can specify the full path to java.exe in the command line when trying to run the code:

    E:\Software Projects\Test\out\production\Test>"C:\Program Files\Java\jdk-12.0.1\bin\java.exe" com.company.Main