0

Help with solve java.lang.UnsupportedClassVersionError: in Gradle project.

I have a problem running programs in the console, but when I run the program in IntelliJ IDEA everything is fine.

In my opinion, the problem is in build.gradle:

plugins {
    id 'java'
    id 'application'
}

group 'org.example'
version '1.0-SNAPSHOT'

mainClassName = "com.gmail.*.division.Main"

application {
    mainClass.set("com.gmail.*.division.Main")
}

applicationDefaultJvmArgs = ["-Djavafx.embed.singleThread=true"]

sourceCompatibility = JavaVersion.VERSION_16
targetCompatibility = JavaVersion.VERSION_16

repositories {
    mavenCentral()
}

dependencies {

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
    compileOnly group: 'javax', name: 'javaee-api', version: '8.0'
    implementation 'jakarta.enterprise:jakarta.enterprise.cdi-api:3.0.0'
}

jar {
    manifest.attributes('Main-Class': 'com.gmail.*.division.Main')
}

//For javaDoc - skipping a method without a description
if (JavaVersion.current().isJava8Compatible()) {
    allprojects {
        tasks.withType(Javadoc) {
            options.addStringOption('Xdoclint:none', '-quiet')
        }
    }
}

test {
    useJUnitPlatform()
}

task makeJavadoc(type: Jar, dependsOn: javadoc) {
    from docsDir
    archiveClassifier.set("docs")
    description = 'Creates a Javadoc Jar'
}

task sourcesJar(type: Jar, dependsOn: classes) {
    archiveClassifier.set("sources")
    from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    archiveClassifier.set("javadoc")
    from javadoc.destinationDir
}

artifacts {
    archives sourcesJar
    archives javadocJar
}

I use Gradle 7.2 and java 16

Error

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/gmail/*/division/Main 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 52.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    You are apparently compiling with Java 16 but running the program with Java 8. So the question is - what do you do to run it? – Bjørn Vester Nov 03 '21 at 09:55
  • 1
    Your IntelliJ is using java 16, but your system (i.e. when you run it from the console) is using java 8. You can see what version of java the console is using by running the command `java --version` in the console. Changing your system java version to use java 16 will depend on what OS you are using. Please tell us what OS you're using and I can tell you how to update it. (If it's linux, also say which distribution) – Pickled Brain Nov 03 '21 at 11:59
  • Pickled Brain - thank you a lot! – Vitalii Smahlenko Nov 03 '21 at 16:04

1 Answers1

0

Changing the %JAVA_HOME% Environment variable to be like the IntelliJ should fix this issue .

You are probably using java 16 with IntelliJ and another version with command line and.

You can use command line if your using windows and type

echo %JAVA_HOME%

then change to IntelliJ IDEA version .

IntelliJ stores the JDK version used by the project within its Project Structure. So to check what version you are using There are two ways to locate this:

  1. Via menu navigation: Navigating to File -> Project Structure
  2. Via keyboard shortcut: For OSX, we press ⌘ + ; For Windows, we press Ctrl + Shift + Alt + S

then you can go to command line if your using windows :

setx -m JAVA_HOME "C:\Path\to\IntelliJ\version"
setx -m PATH "%PATH%;%JAVA_HOME%\bin";
George
  • 2,292
  • 2
  • 10
  • 21