43

Below is the content of the build.gradle file:

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 
}

And below is the result of running the Gradle jar task:

[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] -----------------------------------------------------

Although it claims that the build was successful, the warning has me a little worried about run-time complications. How do I resolve this warning?

Also, I'm using Eclipse Indigo. And I have the Java compiler and build path of my project set to jdk1.6.0_35.

If anyone needs more information, please let me know!


UPDATE

I navigated to Window > Preferences > Gradle and I set "Java Home" to the "Workspace JRE" jdk1.6.0_35,

enter image description here

And now I get the following output when I run the Gradle jar task,

[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] -----------------------------------------------------

What do?

mre
  • 43,520
  • 33
  • 120
  • 170

4 Answers4

15

You can bootstrap class path using bootClasspath option:

apply plugin: 'java'

sourceCompatibility = 1.6
targetCompatibility = 1.6

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

To set the bootClasspath option on all compile tasks in the project you can use the withType() method on the TaskContainer to find all tasks of type Compile:

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

See documentation for details.

Rudik Krasniynos
  • 581
  • 5
  • 11
  • 1
    The documentation of `CompileOptions.bootClasspath` says `Only takes effect if fork is true`. Regarding `fork`: `Defaults to false`. So I'm afraid your example is missing something. – Paolo Fulgoni Mar 26 '14 at 10:33
  • 6
    Hardcoding the JDK path in the build script looks a very bad practice. – Paolo Fulgoni Mar 26 '14 at 10:37
  • I agree with you. But it is only sample. – Rudik Krasniynos Apr 03 '14 at 15:07
  • @PaoloFulgoni Check it. It is working. (Gradle v1.11) – Rudik Krasniynos Apr 03 '14 at 15:12
  • 1
    @PaoloFulgoni: I think Rudik meant to define `JDK6_HOME` in `$GRADLE_USER_HOME/gradle.properties` so it can be configured on a per-user basis and not checked into the VCS. – Rusty Shackleford Dec 15 '15 at 21:50
  • A more portable version of this is: `compileJava.options.bootClasspath = org.gradle.internal.jvm.Jvm.current().getJre().getHomeDir().toString() +"/lib/rt.jar"` (this is when a jdk is installed and `java_home` points to a JDK) – Magnus May 24 '16 at 12:40
  • 1
    If you want to avoid build errors if a user doesn't have JDK6_HOME declared you can add this if statement: `if (project.hasProperty('JDK6_HOME')) { options.bootClasspath = "$JDK6_HOME/jre/lib/rt.jar" }` – Matteus Magnusson Dec 14 '16 at 08:43
12

See the javac docs on cross compilation for details but basically it means you can compile against jdk classes that don't exist, or were different, on your target version. For example perhaps you use java.util.Deque but are targeting jdk5.

I don't believe gradle has built in support for setting this. I have found that you need to twiddle the compile task manually. For example

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

Having said all that, you appear to be building on jdk6 for java6 so I would think you can safely ignore the warning. Are you sure gradle is running under jdk6 and not 7?

Matt
  • 8,367
  • 4
  • 31
  • 61
  • First, I appreciate the feedback. Second, I think I'm now running gradle under jdk6, but I'm not sure. Care to take a look at my update? – mre May 22 '13 at 00:43
  • 3
    Your update definitely shows that you're using a v6 compiler with a v7 rt.jar. Basically, you managed to get gradle compiling with the right compiler, but still using the wrong bootclasspath. I don't do eclipse, so I'm afraid I can't help any further than that. – Ryan Stewart May 22 '13 at 01:25
  • run your build in debug mode (pass -d) will tell you exactly what jvm you're using amongst other things. You should also confirm whether this is an eclipse specific issue by running your build from the cli. – Matt May 22 '13 at 09:58
  • 1
    @Ryan Stewart, It turns out that I was specifying the Java 7 JVM in the eclipse.ini file. -_- – mre May 22 '13 at 13:51
3

Rudik's answer needs a small modification to work with Gradle 2.0 and above: The "Compile" property must be changed to "JavaCompile." See: After upgrading to Gradle 2.0: Could not find property 'Compile' on root project

I ran into the original poster's problem on Android Studio 2.2.2 (JDK8) compiling for Google App Engine (Java 7). Here is Rudik's answer accordingly modified to fix this scenario:

apply plugin: 'java'

sourceCompatibility = 1.7
targetCompatibility = 1.7

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

And again in gradle.properties, add whatever your path is to the JDK, e.g. for Mac

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

Thanks again to Rudik for posting the original answer. I wanted to add a comment instead of creating a new answer, but did not have enough reputation points (yet).

Community
  • 1
  • 1
Paul M
  • 31
  • 2
1

The solution to define the JVM for gradle at workspace level is not ideal. In the case of multiple projects/builds on different JVMs, this cannot be used.

In Eclipse Kepler with Spring Gradle plugin (3.3.0) it is possible to define the JVM for your gradle build task (External tool).

At External Tool Configuration | Arguments | Java Home you can define which JVM should be used for the build.

OblongZebra
  • 466
  • 5
  • 16