Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access `java.home` being used by Gradle?

Tags:

gradle

./gradlew properties shows no properties having the value of ${JAVA_HOME} and the following emit an error indicating no such property exists:

println org.gradle.java.home
println gradle.java.home
println java.home

In the end, I want to specify a compile-time dependency on the tools.jar that's part of the JDK. For example:

dependencies {
  compile files("${java.home}/lib/tools.jar")
}
like image 696
Noel Yap Avatar asked Mar 29 '16 01:03

Noel Yap


People also ask

Does Gradle use JAVA_HOME?

Gradle uses whatever JDK it finds in your path. Alternatively, you can set the JAVA_HOME environment variable to point to the installation directory of the desired JDK. See the full compatibility notes for Java, Groovy, Kotlin and Android.

Where can I find Gradle user home?

The default Gradle user home directory is $USER_HOME/. gradle . A property defined in the properties file, in the Gradle user home directory, overrides the property values defined in a properties file in the project directory.

How do I change my Gradle from org to Java home?

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.


3 Answers

println System.properties['java.home']

like image 113
Noel Yap Avatar answered Oct 25 '22 00:10

Noel Yap


It's not very well documented from what I can find, but you can use the Jvm object in Gradle like this:

println org.gradle.internal.jvm.Jvm.current().getJavaHome()

There's a discussion thread here that shows a couple other usages.

Plus, if you just want to address the tools jar, you can use

org.gradle.internal.jvm.Jvm.current().toolsJar
like image 39
ewhoch Avatar answered Oct 24 '22 23:10

ewhoch


If you wish to set the JAVA_HOME used by Gradle you can setting the environment property:

org.gradle.java.home

Specifies the Java home for the Gradle build process. The value can be set to either a jdk or jre location, however, depending on what your build does, jdk is safer. A reasonable default is used if the setting is unspecified.

If you are trying to reference the existing JAVA_HOME being used, you can gain access to the System Environment properties in this way:

java.home = "$System.env.JAVA_HOME"
like image 24
pczeus Avatar answered Oct 25 '22 01:10

pczeus