“executable”属性中的工具链与“javaLauncher”属性中的工具链不匹配

a_s*_*ber 10 intellij-idea

IntelliJ IDEA 2022.3.3(社区版)JDK 1.8

这是我的App.java

public class App {
    public String getGreeting() {
        return "Interesting cases!" +
                "\nJDK: " + System.getProperty("java.version");
    }

    public static void main(String[] args) {
        System.out.println(new App().getGreeting());
    }
}
Run Code Online (Sandbox Code Playgroud)

这里的build.gradle:

/*
 * 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 take a look at the 'Building Java & JVM projects' chapter in the Gradle
 * User Manual available at https://docs.gradle.org/8.0.1/userguide/building_java_projects.html
 * This project uses @Incubating APIs which are subject to change.
 */

plugins {
    // Apply the application plugin to add support for building a CLI application in Java.
    id 'application'
}

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

dependencies {
    // This dependency is used by the application.
    implementation 'com.google.guava:guava:31.1-jre'
}

testing {
    suites {
        // Configure the built-in test suite
        test {
            // Use JUnit Jupiter test framework
            useJUnitJupiter('5.9.1')
        }
    }
}

application {
    // Define the main class for the application.
    mainClass = 'myproject.javatestdocker.App'
}
Run Code Online (Sandbox Code Playgroud)

从终端我成功运行应用程序:

./gradlew run
Run Code Online (Sandbox Code Playgroud)

好的。

但是当我尝试从 IntelliJ IDEA 运行应用程序(通过绿色三角形)时,我收到错误:

> Task :app:App.main() FAILED

Execution failed for task ':app:App.main()'.
> Toolchain from `executable` property does not match toolchain from `javaLauncher` property

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
Run Code Online (Sandbox Code Playgroud)

小智 5

我遇到了同样的问题,使用 Gradle 8.0.2 运行 Java 11

就我而言,唯一的解决方案是降级到 Gradle 7.6.1,如下所示:-

sdk uninstall gradle 8.0.2
sdk install gradle 7.6.1
Run Code Online (Sandbox Code Playgroud)

在我的项目中,我必须重新包装,以便 7.6.1 成为所有活动的 Gradle。

gradle wrapper --gradle-version 7.6.1 --distribution-type all
Run Code Online (Sandbox Code Playgroud)