3

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
CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
Armino
  • 129
  • 1
  • 1
  • 11
  • Do you have [this option enabled](https://i.imgur.com/Kq837wX.png)? What are the [source/target level](https://stackoverflow.com/a/12900859/104891) settings in your project? – CrazyCoder Aug 05 '19 at 21:43
  • I have the "Use compiler for module target..." enabled. Under Project bytecode version, I had nothing selected, so I now tried selecting 12 and 13. Under "Per-module bytecode version:" I selected the module with "Same as language level", but still I get the same error. – Armino Aug 05 '19 at 21:57
  • What IntelliJ IDEA version do you use? Please share the [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – CrazyCoder Aug 05 '19 at 21:57
  • I'm using Intellij IDEA 2019.2. I don't know if there is anything else I can share regarding this topic, besides the stuff above. The example has very little code and produces the same error (well at least on my IDE). P.S. Sorry for a somewhat late response. Additionally, I have tried running the Main function through terminal, and get the error obviously related to the same issue (I'll add this error to the post) – Armino Aug 06 '19 at 20:06
  • Zip and share the project directory. – CrazyCoder Aug 06 '19 at 20:06
  • Oh I see. Unfortunately I don't see a file sharing option here, so here's a link (as I understand this might be unethical for people who will view this post in the future, but I guess we can explain how we solved the problem - even without access to the file) https://www.dropbox.com/s/he51lgd0mh1d8cl/Test.rar?dl=0 – Armino Aug 06 '19 at 20:31
  • I [can't reproduce the problem](https://i.imgur.com/GxmX2Re.png) with this project. Please share idea.log and build.log files (enable debug logging for build.log). See https://intellij-support.jetbrains.com/hc/articles/207241085 for the instructions. Do you run using Java 12 from the command line just like in IntelliJ IDEA? – CrazyCoder Aug 06 '19 at 20:34
  • Does this answer your question? [How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version](https://stackoverflow.com/questions/10382929/how-to-fix-java-lang-unsupportedclassversionerror-unsupported-major-minor-versi) – 9ilsdx 9rvj 0lo Jun 07 '21 at 11:17

3 Answers3

3

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
CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • I was able to run it using the full command as you said. I've adjusted the target level to 12, but still can't use it unless I run the full command. But I guess we've tracked my problem, here's what I get when running java -version: openjdk version "11.0.3" 2019-04-16 OpenJDK Runtime Environment (build 11.0.3+12-b304.10) OpenJDK 64-Bit Server VM (build 11.0.3+12-b304.10, mixed mode) – Armino Aug 10 '19 at 15:21
  • Now no matter what I change in Project Settings/Settings, it still stays 11.0.3. But I think this might be the solution, because when I go to "Switch Boot Runtime" or "Switch Boot JDK" it offers me 11.0.3 and 12.0.1, and my JDK has currently 11.0.3 selected in both options https://intellij-support.jetbrains.com/hc/en-us/articles/206544879-Selecting-the-JDK-version-the-IDE-will-run-under – Armino Aug 10 '19 at 15:23
  • When I open "Switch IDE Boot JDK" it gives me the following warning: "Changing these values may cause unwanted bahavior of IntelliJ IDEA. Please do not change these unless you have been asked". Would it be safe for me to try to change from 11.0.3 to 12.0.1 in "Switch IDE Boot JDK" and/or "Switch Boot Runtime"? – Armino Aug 10 '19 at 15:25
  • Update: I've changed the "Switch Boot Runtime" from build 11.0.3 to build 12.0.1 and everything works now! When I go to "Switch IDE Boot JDK", instead of 11.0.3, it says "" now. I have the option to choose 12.0.1 there as well - do you recommend changing this too? – Armino Aug 10 '19 at 17:00
  • @Armino If you run it from IntelliJ IDEA Terminal, it would be a known issue: https://youtrack.jetbrains.com/issue/IDEA-218032. If you run it outside of IntelliJ IDEA, you need to change PATH instead of the boot runtime. – CrazyCoder Aug 10 '19 at 18:57
0

I had this issue when my 'Run/Debug Configurations' were set to a different version.

I updated them to default to the module level and it solved it for me.

Code Eyez
  • 313
  • 3
  • 14
-2

Solved by editing file "idea.properties" and setting proper JVMVersion