# Changing Java Version inside CMake

**URL:** https://discourse.cmake.org/t/changing-java-version-inside-cmake/3541
**Category:** Code
**Tags:** os:linux, gen:makefiles, os:android, lang:java
**Created:** [June 9, 2021, 2:43pm UTC](https://discourse.cmake.org/t/changing-java-version-inside-cmake/3541 "2021-06-09T14:43:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![inevo](https://discourse.cmake.org/user_avatar/discourse.cmake.org/inevo/32/1469_2.png) [@inevo](https://discourse.cmake.org/u/inevo)
#### Post date: [June 9, 2021, 2:43pm UTC](https://discourse.cmake.org/t/changing-java-version-inside-cmake/3541/1 "2021-06-09T14:43:22Z")

</div>

Hi, I need to cross-compile a jar and a shared library so that they will be imported into an Android application and ran on a machine running Ubuntu. For this reason I would like to create a CMakeLists.txt file that will create two different jars and two different shared libraries, one for each OS.  
I have two JDK’s on my machine atm, Java 8 and Java 15 and I want one jar to be created using Java 8 and the other with Java 15 (The java code is compatible with java 8). Is there anyway I can change the Java version within the CMakeLists.txt file? I tried changing the JAVA\_HOME with no success.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [June 9, 2021, 11:38pm UTC](https://discourse.cmake.org/t/changing-java-version-inside-cmake/3541/2 "2021-06-09T23:38:22Z")

</div>

CMake does not support multiple toolchains for a single language within a given project. You’ll need to compile the source twice: once with Java 8 and again with Java 15. You can use [`ExternalProject`](https://cmake.org/cmake/help/latest/module/ExternalProject.html) to do this within a single “superproject” though.

---

<div class="post-metadata">

### Author: ![inevo](https://discourse.cmake.org/user_avatar/discourse.cmake.org/inevo/32/1469_2.png) [@inevo](https://discourse.cmake.org/u/inevo)
#### Post date: [June 13, 2021, 8:00am UTC](https://discourse.cmake.org/t/changing-java-version-inside-cmake/3541/3 "2021-06-13T08:00:38Z")

</div>

Assuming I will use ExternalProject, how do I change the Java version when running `make` on the root directory CMakeLists.txt file? FindJava takes the java version configured in JavaHome and I can’t find a way to bypass this.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [June 14, 2021, 11:24am UTC](https://discourse.cmake.org/t/changing-java-version-inside-cmake/3541/4 "2021-06-14T11:24:02Z")

</div>

You’ll probably have to pass `JAVA_HOME` to each project separately. You can either wrap the configure command in a script to set the environment or see if there’s some way to set it via CMake variables instead.
