新项目中的Java LinkerError

问题描述 投票:0回答:1

我使用 JetBrains IntelliJ Idea 创建了新项目

gradle init --use-defaults --type java-application

然后我尝试构建并运行它,但收到此错误:

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

使用时项目也运行成功

gradle run

这是我的 build.gradle.kts 文件:

/*
 * 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()
}

这是settings.build.kts 文件:

/*
 * 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")

我尝试在Idea中更改java版本,设置$JAVA_HOME变量

java macos gradle intellij-idea linker-errors
1个回答
0
投票

尽管您说您在 IntelliJ IDEA 中配置了 Java 版本,但确保 Gradle 构建过程也使用正确的 Java 版本至关重要。 在 Gradle Build 中,提供 Java 版本: 您已使用以下内容在您的

build.gradle.kts
中提供 Java 语言版本:

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

验证您的项目是否与提供的 Java 语言版本兼容(在本示例中为 21)。如果您使用不同版本的软件,请更新 Java。

另外,寻找:-

File -> Project Structure -> Project 

项目SDK与Java版本匹配

File -> Project Structure -> Modules

模块SDK与Java版本匹配

这些是一些需要注意的小细节。因为代码看起来不错。另外,尝试在 IntelliJ IDEA 中重建并运行您的项目。

© www.soinside.com 2019 - 2024. All rights reserved.