Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
  }
}
like image 457
Gerrit Brouwer Avatar asked Mar 27 '14 08:03

Gerrit Brouwer


People also ask

How do I run a Gradle with a specific version of Java?

Right click on the deploy or any other task and select "Open Gradle Run Configuration..." Then navigate to "Java Home" and paste your desired java path.

What is compileJava in Gradle?

compileJava — JavaCompile. Depends on: All tasks which contribute to the compilation classpath, including jar tasks from projects that are on the classpath via project dependencies. Compiles production Java source files using the JDK compiler. processResources — ProcessResources.

Does Gradle 7 support java11?

A Java version between 8 and 18 is required to execute Gradle. Java 19 and later versions are not yet supported.

What is sourceCompatibility in build Gradle?

According to Gradle documentation: sourceCompatibility is "Java version compatibility to use when compiling Java source." targetCompatibility is "Java version to generate classes for."


3 Answers

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.

like image 199
Oleg Estekhin Avatar answered Oct 23 '22 03:10

Oleg Estekhin


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;

  • The options are only set if required
  • All the JAR files are added to the bootClasspath
  • The extensionDirs is also set (see http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#extdirs-option)

.

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/"
        }
    }
}
like image 4
gdt Avatar answered Oct 23 '22 03:10

gdt


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

like image 1
Lóránt Pintér Avatar answered Oct 23 '22 03:10

Lóránt Pintér