Gradle compileJava 任务警告:[options] 引导类路径未与 -source 1.6 一起设置

Posted

技术标签:

【中文标题】Gradle compileJava 任务警告:[options] 引导类路径未与 -source 1.6 一起设置【英文标题】:Gradle compileJava task warning: [options] bootstrap class path not set in conjunction with -source 1.6 【发布时间】:2013-05-16 18:52:12 【问题描述】:

以下是build.gradle文件的内容:

apply plugin: 'java'

archivesBaseName    = 'foo-bar'
version             = '1.0'
sourceCompatibility = JavaVersion.VERSION_1_6
targetCompatibility = JavaVersion.VERSION_1_6

sourceSets.main.resources.exclude 'foo.jks'

compileJava
    println project.sourceCompatibility 
    println project.targetCompatibility 
    println sourceCompatibility 
    println targetCompatibility 

下面是运行 Gradle jar 任务的结果:

[sts] -----------------------------------------------------
[sts] Starting Gradle build for the following tasks: 
[sts]      :jar
[sts] -----------------------------------------------------
1.6
1.6
1.6
1.6
:compileJavawarning: [options] bootstrap class path not set in conjunction with -source 1.6
1 warning

:processResources UP-TO-DATE
:classes
:jar

BUILD SUCCESSFUL

Total time: 1 mins 3.072 secs
[sts] -----------------------------------------------------
[sts] Build finished succesfully!
[sts] Time taken: 1 min, 3 sec
[sts] -----------------------------------------------------

虽然它声称构建成功,但警告让我有点担心运行时并发症。如何解决此警告?

另外,我正在使用 Eclipse Indigo。我将项目的 Java 编译器和构建路径设置为 jdk1.6.0_35。

如果有人需要更多信息,请告诉我!


更新

我导航到 Window > Preferences > Gradle 并将“Java Home”设置为“Workspace JRE”jdk1.6.0_35,

现在我在运行 Gradle jar 任务时得到以下输出,

[sts] -----------------------------------------------------
[sts] Starting Gradle build for the following tasks: 
[sts]      :jar
[sts] -----------------------------------------------------
1.6
1.6
1.6
1.6
:compileJavawarning: java\lang\Enum.class(java\lang:Enum.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\lang\Comparable.class(java\lang:Comparable.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\lang\Object.class(java\lang:Object.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\io\Serializable.class(java\io:Serializable.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\lang\String.class(java\lang:String.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\io\InputStream.class(java\io:InputStream.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\security\KeyStore.class(java\security:KeyStore.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: javax\crypto\Cipher.class(javax\crypto:Cipher.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: javax\crypto\spec\SecretKeySpec.class(javax\crypto\spec:SecretKeySpec.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: javax\xml\bind\DatatypeConverter.class(javax\xml\bind:DatatypeConverter.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\lang\CloneNotSupportedException.class(java\lang:CloneNotSupportedException.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\lang\Class.class(java\lang:Class.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\lang\ClassLoader.class(java\lang:ClassLoader.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\security\KeyStore$LoadStoreParameter.class(java\security:KeyStore$LoadStoreParameter.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\io\Closeable.class(java\io:Closeable.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\lang\AutoCloseable.class(java\lang:AutoCloseable.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\lang\Exception.class(java\lang:Exception.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\lang\Throwable.class(java\lang:Throwable.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\security\cert\Certificate.class(java\security\cert:Certificate.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\security\Key.class(java\security:Key.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\security\spec\KeySpec.class(java\security\spec:KeySpec.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: javax\crypto\SecretKey.class(javax\crypto:SecretKey.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\lang\Error.class(java\lang:Error.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\security\KeyStoreException.class(java\security:KeyStoreException.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\security\GeneralSecurityException.class(java\security:GeneralSecurityException.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\lang\RuntimeException.class(java\lang:RuntimeException.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\io\IOException.class(java\io:IOException.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\security\NoSuchAlgorithmException.class(java\security:NoSuchAlgorithmException.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\security\cert\CertificateException.class(java\security\cert:CertificateException.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: javax\crypto\NoSuchPaddingException.class(javax\crypto:NoSuchPaddingException.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\security\InvalidKeyException.class(java\security:InvalidKeyException.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\security\KeyException.class(java\security:KeyException.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\nio\ByteBuffer.class(java\nio:ByteBuffer.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: javax\crypto\IllegalBlockSizeException.class(javax\crypto:IllegalBlockSizeException.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: javax\crypto\BadPaddingException.class(javax\crypto:BadPaddingException.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\lang\StringBuilder.class(java\lang:StringBuilder.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\lang\AbstractStringBuilder.class(java\lang:AbstractStringBuilder.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\lang\CharSequence.class(java\lang:CharSequence.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: java\lang\StringBuffer.class(java\lang:StringBuffer.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
39 warnings

:processResources UP-TO-DATE
:classes
:jar

BUILD SUCCESSFUL

Total time: 1.96 secs
[sts] -----------------------------------------------------
[sts] Build finished succesfully!
[sts] Time taken: 0 min, 1 sec
[sts] -----------------------------------------------------

做什么?

【问题讨论】:

检查你的 JAVA_HOME 环境变量 【参考方案1】:

您可以使用 bootClasspath 选项引导类路径:

apply plugin: 'java'

sourceCompatibility = 1.6
targetCompatibility = 1.6

compileJava.options.bootClasspath = "$JDK6_HOME/jre/lib/rt.jar"

要为项目中的所有编译任务设置 bootClasspath 选项,您可以使用 TaskContainer 上的 withType() 方法来查找所有编译类型的任务:

apply plugin: 'java'

sourceCompatibility = 1.6
targetCompatibility = 1.6

tasks.withType(JavaCompile) 
    options.bootstrapClasspath = files("$JDK6_HOME/jre/lib/rt.jar")

gradle.properties:

JDK6_HOME=C:/JAVA/jdk6

详情请见documentation。

【讨论】:

CompileOptions.bootClasspath 的文档说Only takes effect if fork is true。关于forkDefaults to false。所以我担心你的例子遗漏了一些东西。 在构建脚本中硬编码 JDK 路径看起来是一种非常糟糕的做法。 我同意你的看法。但这只是样本。 @PaoloFulgoni:我认为 Rudik 的意思是在 $GRADLE_USER_HOME/gradle.properties 中定义 JDK6_HOME,因此它可以基于每个用户进行配置,而不是签入 VCS。 如果你想在用户没有声明 JDK6_HOME 的情况下避免构建错误,你可以添加这个 if 语句:if (project.hasProperty('JDK6_HOME')) options.bootClasspath = "$JDK6_HOME/jre/lib/rt.jar" 【参考方案2】:

Rudik 的回答需要稍作修改才能与 Gradle 2.0 及更高版本一起使用:“Compile”属性必须更改为“JavaCompile”。见:After upgrading to Gradle 2.0: Could not find property 'Compile' on root project

我在为 Google App Engine (Java 7) 编译 android Studio 2.2.2 (JDK8) 时遇到了原始发帖人的问题。以下是 Rudik 的答案,经过相应修改以解决这种情况:

apply plugin: 'java'

sourceCompatibility = 1.7
targetCompatibility = 1.7

tasks.withType(JavaCompile) 
    options.bootClasspath = "$JDK7_HOME/jre/lib/rt.jar"

再次在 gradle.properties 中,将您的路径添加到 JDK,例如适用于 Mac

JDK7_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home

再次感谢 Rudik 发布原始答案。我想添加评论而不是创建新答案,但没有足够的声望点(还)。

【讨论】:

【参考方案3】:

在工作空间级别为 gradle 定义 JVM 的解决方案并不理想。如果在不同的 JVM 上有多个项目/构建,则无法使用。

在带有 Spring Gradle 插件 (3.3.0) 的 Eclipse Kepler 中,可以为您的 gradle 构建任务(外部工具)定义 JVM。

外部工具配置 | 参数 | Java Home您可以定义应该使用哪个 JVM 进行构建。

【讨论】:

【参考方案4】:

请参阅cross compilation for details 上的 javac 文档,但基本上这意味着您可以针对目标版本上不存在或不同的 jdk 类进行编译。例如,也许您使用java.util.Deque,但目标是 jdk5。

我不相信 gradle 已内置支持设置此项。我发现您需要手动调整编译任务。例如

def bootClasspathStr = "$yourJavaVersionXInstallationPath/jre/lib/rt.jar"
project.tasks.withType(AbstractCompile,  AbstractCompile ac ->
    ac.options.bootClasspath = bootClasspathStr // options is always there but not defined on AbstractCompile so going to hit it anyway
)

说了这么多,您似乎是在 jdk6 上为 java6 构建的,所以我认为您可以放心地忽略该警告。你确定 gradle 是在 jdk6 而不是 7 下运行的吗?

【讨论】:

首先,感谢您的反馈。其次,我想我现在在 jdk6 下运行 gradle,但我不确定。想看看我的更新吗? 您的更新明确表明您正在使用带有 v7 rt.jar 的 v6 编译器。基本上,您设法使用正确的编译器进行 gradle 编译,但仍然使用错误的引导类路径。我不做日食,所以恐怕我无能为力了。 在调试模式下运行你的构建(pass -d)会告诉你你正在使用什么jvm。您还应该通过从 cli 运行构建来确认这是否是特定于 Eclipse 的问题。 @Ryan Stewart,原来我在 eclipse.ini 文件中指定了 Java 7 JVM。 -_-

以上是关于Gradle compileJava 任务警告:[options] 引导类路径未与 -source 1.6 一起设置的主要内容,如果未能解决你的问题,请参考以下文章

错误:Gradle:任务“:core:compileJava”执行失败。 > 编译失败

使用 ./gradlew 运行任务 ':app:compileJava' 执行失败

任务 ':compileJava' 执行失败。 > 无效的源版本:1.7

无法运行gradle

查找在gradle中运行编译器的JDK的位置

Spring Boot gradle build - 无效的源版本:11