Setting path to JDK

(Nelson) #1

How do I set the path to a JDK ? I can’t want to use the envirometns JDK that JAVA_HOME points to. Is there a way to say in my gradle script where the JDK is found ?

(Peter Niederwieser) #2

If you just want to use a different javac, you can do so by setting 'compile.options.fork = true; compile.options.forkOptions.executable=‘path/to/javac’. If you want to run Gradle with a different JDK and you are using the Gradle daemon, you can set ‘’ in ‘’. Soon, this setting will also be honored in non-daemon mode. Meanwhile you could tweak the ‘gradle(w)’ script to set JAVA_HOME according to your needs.

(Nelson) #3

Hi Peter ,

thx for your anwser. What I don’t get so far is where this has to be put. I just started to investigate how to switch our project here from ant to gradle. And so far I want just one thnig to happen -> compile our java sources.

Therefore I need to do these things:

Set the compile classpath which in my project is under “project/lib/jar” Change from the default gradle src folde to my project src folder which is under “project/src”

I just started a build file from scratch :

apply plugin: ‘java’ apply plugin: ‘eclipse’

sourceCompatibility = ‘1.5’ targetCompatibility = ‘1.5’ version = ‘1.0’

sourceSets {

main {

java {

srcDir ‘src’


resources {

srcDir ‘web’




Can u help me with this ?


(Peter Niederwieser) #4

First you’d have to explain what exactly you are trying to accomplish.

(Nelson) #5

What I want to do is the following: I want to compile the java classes of my project. My src folder is under “MyProject”/src. The jars that I need on the classpath are found under “MyProject”/lib/jar.

So I need to change the source folder from the gradle default (main/java/src) to only src plus set the classpath to “MyProject”/lib/jar.

Plus I need to set a path to my java compiler somehow because I can’t use the one references over JAVA_HOME. (compile.options.forkOptions.executable=‘path/to/javac’)

(Peter Niederwieser) #6

I recommend to take some time to study the Gradle user guide. You’ll want something like this:

apply plugin: "java" = ["MyProject/src"]
dependencies {
  compile fileTree(dir: "MyProject/lib/jar")
  tasks.withType(JavaCompile) {
    options.fork = true
    options.forkOptions.executable = "path/to/javac"

(Giovanni Botta) #7

How do you do this in version 1.9?

(Peter Niederwieser) #8

It’s the same, but my code snippet had some inaccuracies (fixed). Note that this will slow down the build, compared to running Gradle with the “right” JDK.