# Can I declare JDK as a dependency?

**URL:** <https://discuss.gradle.org/t/can-i-declare-jdk-as-a-dependency/14690>\
**Category:** Help/Discuss\
**Created:** [February 22, 2016, 10:25pm UTC](https://discuss.gradle.org/t/can-i-declare-jdk-as-a-dependency/14690 "2016-02-22T22:25:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![EricSalemi](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ericsalemi/32/9734_2.png) [@EricSalemi](https://discuss.gradle.org/u/EricSalemi)\
**Post date:** [February 22, 2016, 10:25pm UTC](https://discuss.gradle.org/t/can-i-declare-jdk-as-a-dependency/14690/1 "2016-02-22T22:25:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Lance\_Java](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance_java/32/787_2.png) [@Lance\_Java](https://discuss.gradle.org/u/Lance_Java)\
**Post date:** [February 23, 2016, 11:20am UTC](https://discuss.gradle.org/t/can-i-declare-jdk-as-a-dependency/14690/2 "2016-02-23T11:20:25Z")

</div>

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](http://openjdk.java.net) 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](https://www.jfrog.com/open-source/#os-arti))

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](https://docs.oracle.com/javase/7/docs/api/javax/tools/JavaCompiler.html))

There’s also [JDT](https://eclipse.org/jdt/core/) which is Eclipse’s embedded compiler and [beanshell](http://www.beanshell.org) (interprets java scripts at runtime rather than compiling .class files)

I’m interested to see your progress with this

---

<div class="post-metadata">

**Author:** ![st\_oehme](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/st_oehme/32/5249_2.png) [@st\_oehme](https://discuss.gradle.org/u/st_oehme)\
**Post date:** [February 23, 2016, 2:43pm UTC](https://discuss.gradle.org/t/can-i-declare-jdk-as-a-dependency/14690/3 "2016-02-23T14:43:59Z")

</div>

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

```gradle
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"
}

```

---

<div class="post-metadata">

**Author:** ![Lance\_Java](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance_java/32/787_2.png) [@Lance\_Java](https://discuss.gradle.org/u/Lance_Java)\
**Post date:** [February 23, 2016, 3:10pm UTC](https://discuss.gradle.org/t/can-i-declare-jdk-as-a-dependency/14690/4 "2016-02-23T15:10:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![EricSalemi](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ericsalemi/32/9734_2.png) [@EricSalemi](https://discuss.gradle.org/u/EricSalemi)\
**Post date:** [February 27, 2016, 11:31am UTC](https://discuss.gradle.org/t/can-i-declare-jdk-as-a-dependency/14690/5 "2016-02-27T11:31:22Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![st\_oehme](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/st_oehme/32/5249_2.png) [@st\_oehme](https://discuss.gradle.org/u/st_oehme)\
**Post date:** [March 1, 2016, 3:55pm UTC](https://discuss.gradle.org/t/can-i-declare-jdk-as-a-dependency/14690/6 "2016-03-01T15:55:27Z")

</div>

> [@EricSalemi](#):
>
> The Gradle documentation states somewhere that using the Groovy compiler to compile pure java code is not recommended. Why is that?

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

> [@EricSalemi](#):
>
> I also noticed that there is a possibility to select a “toolchain” for a plugin.

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.

---

<div class="post-metadata">

**Author:** ![cobexer](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/cobexer/32/10563_2.png) [@cobexer](https://discuss.gradle.org/u/cobexer)\
**Post date:** [September 26, 2018, 8:53pm UTC](https://discuss.gradle.org/t/can-i-declare-jdk-as-a-dependency/14690/7 "2018-09-26T20:53:50Z")

</div>

I just found out about official openJDK binaries on [http://jdk.java.net/11/](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?

😃
