【问题标题】:gradle java9 Could not target platform: 'Java SE 9' using tool chain: 'JDK 8 (1.8)'gradle java9 无法定位平台:'Java SE 9' 使用工具链:'JDK 8 (1.8)'
【发布时间】:2020-09-05 08:40:27
【问题描述】:

我想在 Eclipse 氧气中的 gradle 项目中使用 java9。当我 运行:

Run as> Gradle Test on  GreeterTest.java

使用以下代码:

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());
    }
}

我测试的课程是:

package hello;

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

我收到错误消息

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

我的 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.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
-Dosgi.instance.area.default=@user.home/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

I have added the build path

I have change the compile parameter

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

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'
}

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

【问题讨论】:

  • 使用./gradlew clean build 的cli 怎么样,对你有用吗?
  • 使用最新的 Intellij,对我来说似乎工作正常。
  • 您的机器上没有安装Java9 SDK,也没有设置为JAVA_HOME + JAVA_PATH

标签: java gradle java-9 eclipse-oxygen


【解决方案1】:

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

保持一致
JAVA_HOME=/path/to/jdk9

在 MacOSX 中,类似于:

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

作为informed in comments,Linux 上的默认路径是:

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

【讨论】:

    【解决方案2】:

    据我所知,这是 Gradle 版本问题。 (Gradle and Java 9 compatibility issue)。

    你需要将wrapper升级到4.3.1,cli ref:

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

    让我知道这是否有效。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • buidship插eclips氧气没有升级到JAVA 9所以我必须使用Java8
      • 尝试在运行配置的 Java 主页选项卡上设置 JDK 位置并升级包装器。
      【解决方案4】:

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

      【讨论】:

      • 谢谢,有帮助!
      【解决方案5】:

      除了你尝试过的其他方式:

      Gradle 在您的 %userprofile%/.gradle/gradle.properties 中设置了默认 JVM。 这是我的文件夹“C:\Users\j.gradle\gradle.properties”:

      org.gradle.java.home=C:/Program Files/AdoptOpenJDK/jdk-11.0.7.10-hotspot
      

      您还应该将较新的 JVM 放在此处,以确保 gradle 可以编译较新的项目。

      【讨论】:

        【解决方案6】:

        我为解决此问题所做的工作是在我导入的项目 build.gradle 上的 compileJava 部分执行此操作,我将 sourceCompatibility 和 targetCompatibility 更改为 8。它适用于我。当您从不同版本的其他资源导入项目时会发生这种情况。

        【讨论】:

          【解决方案7】:

          在 Eclipse 中确保您对 Gradle 构建配置进行了适当的设置。

          【讨论】:

            【解决方案8】:

            我在使用不同的 JDK 版本时遇到了同样的错误,所以我使用了以下代码,它运行良好。

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

            【讨论】:

              【解决方案9】:

              我在 eclipse 中遇到了类似的问题,我必须在 Properties -> Gradle 中设置 JAVA_HOME。

              【讨论】:

                【解决方案10】:

                对我来说问题是我用过

                compileOptions {
                    sourceCompatibility JavaVersion.VERSION_1_10
                    targetCompatibility JavaVersion.VERSION_1_10
                }
                

                但该项目是为 Java 1.8 配置的。所以我把这些改成 1_8 就可以了。

                【讨论】:

                  猜你喜欢
                  • 2021-01-17
                  • 1970-01-01
                  • 2017-10-15
                  • 2022-12-10
                  • 2021-03-27
                  • 1970-01-01
                  • 1970-01-01
                  • 2023-02-20
                  • 2012-06-07
                  相关资源
                  最近更新 更多