有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我想在eclipse oxygen内部的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!";
  }
}

我收到了错误信息

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

我的日食。它是

-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

I have set the compile parameter in the 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'
}

What must I do to get this to run?


共 (3) 个答案

  1. # 1 楼答案

    除了您尝试的其他方法之外:

    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可以编译新的项目

  2. # 2 楼答案

    从我看来,这是格拉德尔版本的问题。(Gradle and Java 9 compatibility issue

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

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

    如果有效,请告诉我

  3. # 3 楼答案

    我解决这个问题的方法是在导入的项目构建中这样做。gradle在compileJava第一节将sourceCompatibility和targetCompatibility更改为8。这对我有用。当您从其他资源导入项目时会发生这种情况,这些资源是在不同版本中生成的