I created new project in JetBrains IntelliJ Idea using
gradle init --use-defaults --type java-application
Then I tried to build and run it but got this error:
LinkageError occurred while loading main class org.example.App
Execution failed for task ':app:App.main()'.
> Process 'command '/Users/***/Library/Java/JavaVirtualMachines/corretto-17.0.10/Contents/Home/bin/java'' finished with non-zero exit value 1
Also project runs successfully when using
gradle run
Here is my build.gradle.kts file:
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.6/userguide/building_java_projects.html in the Gradle documentation.
*/
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
application
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Use JUnit Jupiter for testing.
testImplementation(libs.junit.jupiter)
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
// This dependency is used by the application.
implementation(libs.guava)
}
// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
application {
// Define the main class for the application.
mainClass = "org.example.App"
}
tasks.named<Test>("test") {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}
and here is settings.build.kts file:
/*
* This file was generated by the Gradle 'init' task.
*
* The settings file is used to specify which projects to include in your build.
* For more detailed information on multi-project builds, please refer to https://docs.gradle.org/8.6/userguide/multi_project_builds.html in the Gradle documentation.
*/
plugins {
// Apply the foojay-resolver plugin to allow automatic download of JDKs
id("org.gradle.toolchains.foojay-resolver-convention") version "0.7.0"
}
rootProject.name = "educationproject-ml"
include("app")
I tried to change java version in Idea, set $JAVA_HOME variable
Although you said that you configured the Java version in IntelliJ IDEA, it's crucial to make sure that the right Java version is also being used by the Gradle build process.
In the Gradle Build, provide the Java version:
You have used the following to provide the Java language version in your build.gradle.kts
:
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
Verify that your project is compatible with the provided Java language version (in this example, 21). Update Java if you are using a different version of the software.
also, look for:-
File -> Project Structure -> Project
Project SDK matches the Java version
File -> Project Structure -> Modules
Module SDK matches the Java version
These are some of the small details that need attention. because code seems to be good. Also, try rebuilding and running your project in IntelliJ IDEA.