How do I tell Gradle to use specific JDK version?
Two ways
- In
gradle.properties
in the.gradle
directory in yourHOME_DIRECTORY
setorg.gradle.java.home=/path_to_jdk_directory
or:
In your
build.gradle
compileJava.options.fork = true
compileJava.options.forkOptions.executable = '/path_to_javac'
Which installed JDK used during Gradle build process
You can add a task that prints what you need when executed (Kotlin DSL):
tasks {
val j by creating {
doLast {
println(System.getProperty("java.home"))
}
}
}
Groovy DSL:
tasks.register("j") {
doLast {
println System.getProperty("java.home")
}
}
Then executing ./gradlew j
:
/usr/lib/jvm/java-8-openjdk/jre
Why could gradlew
use another JVM? Take a look at this script and you'll see that it uses JAVA_HOME
variable to search for JVM. So probably the version from your PATH
is not the same, that JAVA_HOME
is pointing to.
Gradle JVM Version vs JDK
The version reported by gradle -version
is the one of the client VM executing that command.
Normally, Gradle will otherwise use a daemon process to execute your build. That process will respect the org.gradle.java.home
setting.
Note that you can also have a finer control on which Java executable is used for the different tasks in Gradle. See the documentation for details.
gradlew uses wrong Java version
A copy of the latest OpenJDK comes bundled with Android Studio 2.2 and higher, and this is the JDK version we recommend you use for your Android projects.
https://developer.android.com/studio/intro/studio-config.html#jdk
This however, is only for building your apps within Android Studio, not from the terminal with gradlew
, otherwise, it'll use whatever is on your OS's $PATH
variable.
In order to use the embedded JDK, you at least need to set JAVA_HOME
, for example on Linux/Mac,
$ export JAVA_HOME=/path/to/AndroidStudio/jdk # TODO: Find this
$ ./gradlew
My recommendation, however, is to use the mechanism for your OS for installing Java.
For easy Java library management (on Linux & Mac), you can try using sdkman
How do I know for sure which version of JAVAC Gradle used in a build?
Gradle is not actually using javac
(the executable). Rather, it uses the compiler classes programmatically.
By default, Gradle will use the classes from the JDK you use for running Gradle. You can check the version by running gradlew --version
. This also goes for the Javadoc
and JavaExec
task (and similar Java-related tasks and methods).
It is possible to fork the these tasks and use a different JDK than for running Gradle. But this has to be configured in the build script and if you did that, then you probably also know already what you made them use. If this is not your project and you need to be sure, search for "forkOptions" (old and complicated way to do it) and "toolchain" (new and easy way).
I am not aware of a way to make Gradle print the version of Java it will use for a given Java-related task.
In Gradle, is there a way to set JDK to the Eclipse Default?
I'm still not completely sure what you are asking for, but here's a few different takes on it.
If what you want to do is have your code (in Gradle and Eclipse) compile so that the bytecode is compatible with a specific version of Java, use something like this. This does not change the version of Java that either Gradle or Eclipse uses during compilation, just makes the end result "bytecode compatible" with the version you specify. The settings that Luis mentions default to the values set at the more general Java plugin level.
sourceCompatibility = '1.6' //or '1.5' or '1.7', etc.
By default, the Gradle Eclipse plugin will generate the following entry in your
.classpath
file. I believe this always points to the default you specify in Eclipse, but I may be wrong.<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER" exported="true"/>
If you want to change what that container is, pick the one you are looking for in Eclipse and then look in the
.classpath
file for the correct container value. Then you can specify it in the build file:eclipse.classpath.containers 'whatever the container value is'
However if what you want is to be able to change the JAVA_HOME that Gradle runs with to match the default chosen in Eclipse, I think you'll have a tough time. I'm not sure if there's a easy place to find that value programmatically. You could probably set it up from the opposite direction though. Have the developers set JAVA_HOME to match what their Eclipse default is. Then they can reference the JAVA_HOME environment variable in the Eclipse config for their JRE.
Related Topics
How to Pretty Print Xml from Java
Overriding the Java Equals() Method - Not Working
Should I Always Use a Parallel Stream When Possible
Scanning Java Annotations At Runtime
Classpath Including Jar Within a Jar
Appending to an Objectoutputstream
Jcomponents Not Showing Up With Picture Background
How Does a Java Hashmap Handle Different Objects With the Same Hash Code
Get Os-Level System Information
Get Generic Type of Java.Util.List
How to Implement a Single Instance Java Application
How Does the "Final" Keyword in Java Work (I Can Still Modify an Object.)
To Prevent a Memory Leak, the Jdbc Driver Has Been Forcibly Unregistered
Difference Between Javac and the Eclipse Compiler