2

I'm compiling a JNI library with Gradle. It clearly knows how to find a JDK (as the Java half of the project compiles perfectly). However, I'd need a path to the JNI header files in a Gradle native (C++) project.

Is there a way to obtain the path of the JDK being used from a Gradle build file?

Latanius
  • 2,553
  • 2
  • 23
  • 21
  • Isn't `JAVA_HOME` env variable what you need? – Opal Jan 04 '16 at 10:41
  • thanks for the idea; it'd work if it was set, but, for me, it isn't, yet Gradle works well even without it (and it's Gradle's "reasonable defaults" that I'd need). – Latanius Jan 04 '16 at 11:07
  • Did you find a way to get the path to JDK from Gradle? I have the same problem; the answer does not solves the problem (in Android Studio it yields the path to the JRE). Thanks, – piwi Nov 23 '18 at 15:21

2 Answers2

4

According to the top reply here, you can get currently running Java path from the property java.home. In Gradle:

    println System.getProperty('java.home')

or more Groovy:

    println System.properties.'java.home'
Sergey
  • 3,253
  • 2
  • 33
  • 55
RaGe
  • 22,696
  • 11
  • 72
  • 104
  • I'm afraid that ONLY gives the path of the if you are compiling with the JAVA_HOME pointing to a JDK. When builds are cross-platform that information will not match the platform targeted. I just checked our builds use the JDK 7 but JAVA_HOME is Java 8. Something else must load the compiler path from the platform specified for the project. – will May 18 '17 at 02:21
1

Since this is the top answer on Google: Jvm.current().getJavaHome()

Note that the JVM class is internal as of Gradle 6, use it at your own risk.

Sergey
  • 3,253
  • 2
  • 33
  • 55
Alpar
  • 2,807
  • 23
  • 16