Changing Java Version inside CMake

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.

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 to do this within a single “superproject” though.

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.

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.