Home > Software design >  java.lang.UnsupportedClassVersionError: in Gradle project
java.lang.UnsupportedClassVersionError: in Gradle project

Time:11-04

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 gradle.buidl

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)

I would be grateful for any help!!!

CodePudding user response:

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";
  • Related