gradle java9 无法定位平台:'Java SE 9' 使用工具链:'JDK 8 (1.8)'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47500674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:37:41  来源:igfitidea点击:

gradle java9 Could not target platform: 'Java SE 9' using tool chain: 'JDK 8 (1.8)'

javagradlejava-9eclipse-oxygen

提问by stein korsveien

I want to use java9 on my gradle project inside eclipse oxygen. When I run:

我想在 eclipse Oxy 中的 gradle 项目中使用 java9。当我运行时:

Run as> Gradle Test on  GreeterTest.java

with the following code:

使用以下代码:

package hello.test;

import static org.junit.jupiter.api.Assertions.*;    
import org.junit.jupiter.api.Test;    
import hello.Greeter;

class GreeterTest {

    @Test
    void test() {
        Greeter greeter = new Greeter();
        assertEquals("Hello world", greeter.sayHello());
    }
}

and with the class I test as:

和我测试的类:

package hello;

public class Greeter {
  public String sayHello() {
    return "Hello world!";
  }
}

I get the error message

我收到错误消息

Could not target platform: 'Java SE 9' using tool chain: 'JDK 8 (1.8)'.

无法定位平台:“Java SE 9”使用工具链:“JDK 8 (1.8)”。

My eclipse.init is

我的 eclipse.init 是

-startup ../Eclipse/plugins/org.eclipse.equinox.launcher_1.4.0.v20161219-1356.jar
--launcher.library /Users/stein/.p2/pool/plugins/org.eclipse.equinox.launcher.cocoa.macosx.x86_64_.1.550.v20170928-1359
-product org.eclipse.epp.package.jee.product
-showsplash org.eclipse.epp.package.common
--launcher.defaultAction openFile
--launcher.a/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/jre/bin
-vmargs
-Dosgi.requiredJavaVersion=1.9
[email protected]/eclipse-workspace
-XX:+UseG1GC
-XX:+UseStringDeduplication
--add-modules=ALL-SYSTEM
-XstartOnFirstThread
-Dorg.eclipse.swt.internal.carbon.smallFonts
-Dosgi.requiredJavaVersion=1.9
-Xms256m
-Xmx1024m
--add-modules=ALL-SYSTEM
-Xdock:icon=../Resources/Eclipse.icns
-XstartOnFirstThread
-Dorg.eclipse.swt.internal.carbon.smallFonts
-Declipse.p2.max.threads=10
-Doomph.update.url=http://download.eclipse.org/oomph/updates/milestone/latest
-Doomph.redirection.index.redirection=index:/->http://git.eclipse.org/c/oomph/org.eclipse.oomph.git/plain/setups/
-startup ../Eclipse/plugins/org.eclipse.equinox.launcher_1.4.0.v20161219-1356.jar
--launcher.library /Users/stein/.p2/pool/plugins/org.eclipse.equinox.launcher.cocoa.macosx.x86_64_.1.550.v20170928-1359
-product org.eclipse.epp.package.jee.product
-showsplash org.eclipse.epp.package.common
--launcher.defaultAction openFile
--launcher.a/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/jre/bin
-vmargs
-Dosgi.requiredJavaVersion=1.9
[email protected]/eclipse-workspace
-XX:+UseG1GC
-XX:+UseStringDeduplication
--add-modules=ALL-SYSTEM
-XstartOnFirstThread
-Dorg.eclipse.swt.internal.carbon.smallFonts
-Dosgi.requiredJavaVersion=1.9
-Xms256m
-Xmx1024m
--add-modules=ALL-SYSTEM
-Xdock:icon=../Resources/Eclipse.icns
-XstartOnFirstThread
-Dorg.eclipse.swt.internal.carbon.smallFonts
-Declipse.p2.max.threads=10
-Doomph.update.url=http://download.eclipse.org/oomph/updates/milestone/latest
-Doomph.redirection.index.redirection=index:/->http://git.eclipse.org/c/oomph/org.eclipse.oomph.git/plain/setups/

I have added JAVA_HOME

我已经添加了 JAVA_HOME

I have added the build path

我已经添加了构建路径

I have change the compile parameter

我已经改变了编译参数

I have set the compile parameter in the Build.Gradle.

我已经在 Build.Gradle 中设置了 compile 参数。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2'
    }
}

repositories {
    mavenCentral()
}

ext.junit4Version        = '4.12'
ext.junitVintageVersion  = '4.12.2'
ext.junitPlatformVersion = '1.0.2'
ext.junitJupiterVersion  = '5.0.2'
ext.log4jVersion         = '2.9.0'

apply plugin: 'java'
apply plugin: 'eclipse'

apply plugin: 'org.junit.platform.gradle.plugin'

jar {
    baseName = 'junit5-gradle-consumer'
    version = '1.0.0-SNAPSHOT'
}

compileJava {
   sourceCompatibility = 9
   targetCompatibility = 9
}

compileTestJava {
    sourceCompatibility = 9
    targetCompatibility = 9
    options.compilerArgs += '-parameters'
}

junitPlatform {
    // platformVersion '1.0.2'
    filters {
        engines {
            // include 'junit-jupiter', 'junit-vintage'
            // exclude 'custom-engine'
        }
        tags {
            // include 'fast'
            exclude 'slow'
        }
        // includeClassNamePattern '.*Test'
    }
    // configurationParameter 'junit.jupiter.conditions.deactivate', '*'
    // enableStandardTestTask true
    // reportsDir file('build/test-results/junit-platform') // this is the default
    logManager 'org.apache.logging.log4j.jul.LogManager'
}

dependencies {
    // JUnit Jupiter API and TestEngine implementation
    testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}")
    testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")

    // If you also want to support JUnit 3 and JUnit 4 tests
    testCompile("junit:junit:${junit4Version}")
    testRuntime("org.junit.vintage:junit-vintage-engine:${junitVintageVersion}")

    // To avoid compiler warnings about @API annotations in JUnit code
    //testCompileOnly('org.apiguardian:apiguardian-api:1.0.0')

    // To use Log4J's LogManager
    testRuntime("org.apache.logging.log4j:log4j-core:${log4jVersion}")
    testRuntime("org.apache.logging.log4j:log4j-jul:${log4jVersion}")

    // Only needed to run tests in an (IntelliJ) IDE(A) that bundles an older version
    testRuntime("org.junit.platform:junit-platform-   launcher:${junitPlatformVersion}")
}

task wrapper(type: Wrapper) {
    description = 'Generates gradlew[.bat] scripts'
    gradleVersion = '4.1'
}

What must I do to get this to run?

我必须做什么才能让它运行?

回答by chenrui

From what I can see, it is the Gradle version issue. (Gradle and Java 9 compatibility issue).

据我所知,这是 Gradle 版本问题。(Gradle 和 Java 9 兼容性问题)。

You need to upgrade the wrapper to 4.3.1, cli ref:

您需要将包装器升级到 4.3.1,cli 参考:

# upgrade Gradle to 4.3.1 
gradle wrapper --gradle-version 4.3.1 # not ./gradlew

Let me know if that works.

让我知道这是否有效。

回答by Naman

You should probably try to update your JAVA_HOMEin system variables and Java version used in eclipse to be consistent to

您可能应该尝试更新JAVA_HOME系统变量和 eclipse 中使用的 Java 版本以与

JAVA_HOME=/path/to/jdk9

In MacOSX, something like :

在 MacOSX 中,类似于:

JAVA_HOME = /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/bin

As informed in comments, the default path on Linux would be :

评论中所述,Linux 上的默认路径为:

/usr/lib/jvm/java-9-openjdk

回答by stein korsveien

I changed the java_homeand upgraded Gradle. Now it is working.

我更改java_home并升级了 Gradle。现在它正在工作。

回答by tonisives

For me the problem was that I used

对我来说,问题是我用过

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_10
    targetCompatibility JavaVersion.VERSION_1_10
}

but the project was configured for Java 1.8. So I changed these to 1_8 and it works.

但该项目是为 Java 1.8 配置的。所以我将这些更改为 1_8 并且它有效。

回答by Igor Nosovsky

I faced the issue because in IntelliJ Idea in the Settings > Build Tools > Gradle In "Gradle" section, Gradlje JVM used an incorrect Java version. So I specify to use JAVA_HOME and it fixes the issue.

我遇到了这个问题,因为在“设置”>“构建工具”>“Gradle”部分的 IntelliJ Idea 中,Gradlje JVM 使用了不正确的 Java 版本。所以我指定使用 JAVA_HOME 并解决了这个问题。

回答by parrotHyman

I faced the same error with different JDK version, so i used the following code which works fine.

我在使用不同的 JDK 版本时遇到了同样的错误,所以我使用了以下代码,效果很好。

compileJava {
    options.fork = true
    options.forkOptions.javaHome = file('/Library/Java/JavaVirtualMachines/jdk-11.0.5.jdk/Contents/Home')
    targetCompatibility = 11
    sourceCompatibility = 11
}