Can I declare JDK as a dependency?


(Eric Salemi) #1

Hi,

I’m new to gradle and I need to know if what I want to do is possible.

I’m developing a framework in Matlab, for which parts are compiled as C shared libraries or Java classes. People using this framework should not have to install any dependency besides Matlab itself and in particular they should not have to download/install any JDK. Matlab is shipped with a JRE and that what I am starting from.

So far I have been able to use gradle via the tooling API from within Matlab and it works pretty good. I can compile most dependencies (C, C++) but fails to compile pure java code as gradle complains it cannot find “tools.jar”. This behavior seems very logical but I want to know if it is possible to consider the JDK like any other dependency and have gradle download it for me and subsequently use it to build some classes?

Thanks for any tips.


(Lance Java) #2

Not that I’m a lawyer, but there’s likely to be licensing issues with using Oracle’s tools.jar in this way. I’m guessing you could use tools.jar from openjdk without issues.

I’m not sure if the openjdk tools.jars are available in a public repository. If not you could upload various versions of openjdk tools.jar to a repository (eg artifactory)

You could then write a custom JavaCompile task (and java plugin?) which adds a specific version of tools.jar to the classpath before invoking the [java compiler api] (https://docs.oracle.com/javase/7/docs/api/javax/tools/JavaCompiler.html)

There’s also JDT which is Eclipse’s embedded compiler and beanshell (interprets java scripts at runtime rather than compiling .class files)

I’m interested to see your progress with this


(Stefan Oehme) #3

There is no built-in functionality to download the right JDK. You could try something like:

task downloadJdk //write a task that downloads the JDK and extracts it

tasks.withType(JavaCompile) {
  dependsOn downloadJdk
  options.fork = true
  options.bootClasspath = "jdkDownloadPath/lib/rt.jar"
}

(Lance Java) #4

Ah, much nicer / simpler than what I was suggesting.


(Eric Salemi) #5

I will try what Stefan proposed this week-end and report on my finding.

Otherwise, I found out that I can use Groovy compiled classes in Matlab directly and since the groovy compiler seems to be part of Gradle, it would basically solve my issue, with the added benefit that I can take advantage of new funky constructs. The Gradle documentation states somewhere that using the Groovy compiler to compile pure java code is not recommended. Why is that?

I also noticed that there is a possibility to select a “toolchain” for a plugin. Does that relate to the transformation from input to output in Gradle? Does that apply to java compilation, i.e transforming sources to bytecode?


(Stefan Oehme) #6

I’m by no means a Groovy expert, but I would guess that it’s slower than javac.

I guess you saw that in the C/C++ plugins, where we already have toolchain (compiler, linker, etc) discovery. This is not yet the case for Java compilers.


(Christoph Obexer) #7

I just found out about official openJDK binaries on http://jdk.java.net/11/

Licensed GPL, hosted with predictable URL(as of Java 11 apparently), no “I accept license” annoyances.

Soo Gradle could support this starting with Java 11?

:smiley: