CMake编译java代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18985726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CMake to compile java code
提问by STiGMa
Is it possible to use CMake to compile and run java code?
是否可以使用 CMake 来编译和运行 Java 代码?
From command line the commands that I write on terminal is:
从命令行我在终端上写的命令是:
javac -classpath theClasspath mainClass.java
javac -classpath theClasspath mainClass.java
java -classpath theClasspath mainClass
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.
PS:不想生成jar文件;只是为了编译java类,如果可能的话运行它。
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 ">".
更新:我已经更改了命令。我不知道为什么没有显示附加文本。可能是因为我使用了“<”和“>”。
采纳答案by sakra
CMake has somewhat limited support for compiling Java code and executing Java class files.
CMake 对编译 Java 代码和执行 Java 类文件的支持有限。
The standard module FindJavacan be used to find a JDK installed on the local machine. The standard module UseJavaprovides a few functions for Java. Among those is a function add_jar
to compile Java source files to a jar file.
标准模块FindJava可用于查找安装在本地机器上的 JDK。标准模块UseJava为 Java 提供了一些功能。其中包括 add_jar
将 Java 源文件编译为 jar 文件的功能。
Here is a small example that demonstrates how to use add_jar
. Given the Java sample source file HelloWorld.java
:
这是一个演示如何使用add_jar
. 鉴于 Java 示例源文件HelloWorld.java
:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
The following CMake list file will compile HelloWorld.java
to a jar file HelloWorld.jar
and also add a CMake test that runs the jar with the JVM:
以下 CMake 列表文件将编译HelloWorld.java
为 jar 文件HelloWorld.jar
,并添加一个 CMake 测试,该测试使用 JVM 运行 jar:
cmake_minimum_required (VERSION 2.8)
find_package(Java REQUIRED)
include(UseJava)
enable_testing()
project (HelloWorld NONE)
set(CMAKE_JAVA_COMPILE_FLAGS "-source" "1.6" "-target" "1.6")
add_jar(HelloWorld HelloWorld.java)
get_target_property(_jarFile HelloWorld JAR_FILE)
get_target_property(_classDir HelloWorld CLASSDIR)
message(STATUS "Jar file ${_jarFile}")
message(STATUS "Class compiled to ${_classDir}")
add_test(NAME TestHelloWorld COMMAND ${Java_JAVA_EXECUTABLE} -cp ${_jarFile} HelloWorld)
The CMake variable CMAKE_JAVA_COMPILE_FLAGS
can be used to specify compile flags. As a side effect the add_jar
command will set target properties JAR_FILE
and CLASSDIR
that can be used to obtain the path to the generated jar file and the compiled class files directory, respectively.
CMake 变量CMAKE_JAVA_COMPILE_FLAGS
可用于指定编译标志。作为副作用的add_jar
命令将设置目标属性JAR_FILE
和CLASSDIR
可被用于获得的路径所产生的JAR文件和编译的类文件的目录,分别。