Gerrit Brouwer
Gerrit Brouwer

Reputation: 750

How to set Gradle `options.bootClasspath` in an os independent manner?

Because my Java sources and targets must be JRE 1.6 compatible, I need to set options.bootClasspath to a path that contains the 1.6 versions of rt.jar and jce.jar. It must build on both Windows and Unix (Linux/Solaris). What is the proper way to do this? I now use the following approach in my top-level build.gradle, it works, but it seems far from elegant, especially the os-dependent separator : or ;:

import org.apache.tools.ant.taskdefs.condition.Os

subprojects {
  apply plugin: 'java'

  compileJava {
    sourceCompatibility = 1.6
    targetCompatibility = 1.6
    def java6_home = System.getenv("JAVA_HOME_6")
    def java6_lib = "C:/localdata/Program Files (x86)/Java/jdk1.6.0_45/jre/lib/"

    if (java6_home != null) {
      java6_lib = java6_home + "/jre/lib/"
    }

    def sep = ':'
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
      sep = ';'
    }
    options.bootClasspath = java6_lib + "rt.jar" + sep + java6_lib + "jce.jar"
  }
}

Upvotes: 22

Views: 12273

Answers (5)

Since Gradle 4.3 you can use CompileOptions.bootstrapClasspath instead to remove the need for an OS-dependent separator.

Upvotes: 1

Larry Ricker
Larry Ricker

Reputation: 319

I tried to add many jars to my bootClassPath using the instructions above, but never resolved my build issue. I finally resolved the build by setting my JAVA_HOME to point to the IBM JDK 1.7 required by the WebSphere server and adding it to my path. My other projects require Oracle JDK 1.8 so I did not want to make this change permanent.

set JAVA_HOME="C:\Program Files (x86)\IBM\WebSphere\AppServer\java_1.7.1_64\"
set PATH=%JAVA_HOME%\bin;%PATH%
gradle clean war
gradle clean ear deployLocal

Upvotes: 0

gdt
gdt

Reputation: 1903

The accepted answer can work, but if you're using some classes outside java.lang (e.g. javax.crypto.*) you may find you'll get various ClassNotFoundExceptionException's being raised as more JAR files need to be added to the bootClasspath.

To avoid this, I use the following which has the following advantages;

.

tasks.withType(JavaCompile) {
    doFirst {
        if (JavaVersion.toVersion(sourceCompatibility) == JavaVersion.VERSION_1_6
            && JavaVersion.current() != JavaVersion.VERSION_1_6
            && System.env.JDK6_HOME != null) {
            options.fork = true
            options.bootClasspath = fileTree(include: ['*.jar'], dir: "$System.env.JDK6_HOME/jre/lib/").join(File.pathSeparator)
            options.extensionDirs = "$System.env.JDK6_HOME/jre/lib/ext/"
        }
    }
}

Upvotes: 4

Joshua Richardson
Joshua Richardson

Reputation: 2029

A slight modification of the cool solution by Oleg Estekhin above, but doesn't require JDKX_HOME to be set (calculates it on the fly.) Also, modified for doing Java 1.7 builds:

tasks.withType(JavaCompile) {
    doFirst {
        if (sourceCompatibility == '1.7') {
            def JDK7_HOME = "/usr/libexec/java_home -v 1.7".execute().text.trim()
            options.bootClasspath = "$JDK7_HOME/jre/lib/rt.jar"
            options.bootClasspath += "$File.pathSeparator$JDK7_HOME/jre/lib/jsse.jar"
            // use the line above as an example to add jce.jar 
            // and other specific JDK jars
        }
    }
}

Upvotes: 0

Oleg Estekhin
Oleg Estekhin

Reputation: 8405

I am using the following code (assuming the JDK6_HOME points to the root of the JDK 1.6 installation):

tasks.withType(JavaCompile) {
    doFirst {
        if (sourceCompatibility == '1.6' && System.env.JDK6_HOME != null) {
            options.fork = true
            options.bootClasspath = "$System.env.JDK6_HOME/jre/lib/rt.jar"
            options.bootClasspath += "$File.pathSeparator$System.env.JDK6_HOME/jre/lib/jsse.jar"
            // use the line above as an example to add jce.jar 
            // and other specific JDK jars
        }
    }
}

This approach automatically detects the presence of the environment variable and automatically sets the bootClasspath for all modules that declare sourceCompatibility as 1.6.

The options.fork = true is required when you use bootClasspath.

Upvotes: 29

Related Questions