javaintellij-ideaunsupported-class-version

java.lang.UnsupportedClassVersionError: com/dicoding/javafundamental/basic/Main has been compiled by a more recent version of the Java Runtime


enter image description here

I tried to make a package in JAR form. but when I tried it, it failed. how to fix this problem

java -jar out/artifacts/StrukturDasar_jar/StrukturDasar.jar

The Result was this :

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/dicoding/javafundamental/basic/Main has been compiled by a more recent version of the Java Runtime (class file vers
ion 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

My IDE and JDK:

IntelliJ IDEA 2019.1.3 (Community Edition)
Build #IC-191.7479.19, built on May 28, 2019
JRE: 1.8.0_202-release-1483-b58 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0


Solution

  • It happens due to JDK/JRE version mismatch - probably class compiled with higher version JDK, trying to be executed in low version JDK.

    Java Runtime (class file vers ion 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

    Either you have to sort the version mismatch or specify the target version during compilation command.

    This helps you to achieve cross compilation.

    https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#crosscomp-example