Is it possible to use CMake to compile and run java code?
From command line the commands that I write on terminal is:
javac -classpath theClasspath mainClass.java
java -classpath theClasspath mainClass
If so, could you please give me an idea of how this can be achieved?
PS: I do not want to generate a jar file; just to compile the java class and if possible to run it.
Thanks.
Update: I have changed the command. I do not know why the additional text was not displayed. It might be because I used "<" and ">".
CMake has somewhat limited support for compiling Java code and executing Java class files.
The standard module FindJava can be used to find a JDK installed on the local machine. The standard module UseJava provides a few functions for Java. Among those is a function
add_jar
to compile Java source files to a jar file.Here is a small example that demonstrates how to use
add_jar
. Given the Java sample source fileHelloWorld.java
:The following CMake list file will compile
HelloWorld.java
to a jar fileHelloWorld.jar
and also add a CMake test that runs the jar with the JVM:The CMake variable
CMAKE_JAVA_COMPILE_FLAGS
can be used to specify compile flags. As a side effect theadd_jar
command will set target propertiesJAR_FILE
andCLASSDIR
that can be used to obtain the path to the generated jar file and the compiled class files directory, respectively.