CMake编译java代码 [英] CMake to compile java code

查看:2143
本文介绍了CMake编译java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用CMake编译和运行java代码?

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

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:我不想生成一个jar文件;只要编译java类,并尽可能运行它。

PS: I do not want to generate a jar file; just to compile the java class and if possible to run it.

谢谢。

/ strong>:我改变了命令。我不知道为什么不显示附加文本。这可能是因为我使用<和

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对编译Java代码和执行Java类文件的支持有限。

CMake has somewhat limited support for compiling Java code and executing Java class files.

标准模块 FindJava 可以用来查找本地机器上安装的JDK。标准模块 UseJava 为Java提供了一些功能。其中有一个函数 add_jar 可将Java源文件编译为jar文件。

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.

演示如何使用 add_jar 。给定Java示例源文件 HelloWorld.java

Here is a small example that demonstrates how to use add_jar. Given the Java sample source file HelloWorld.java:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

以下CMake列表文件将编译 HelloWorld.java 添加到jar文件 HelloWorld.jar ,并添加一个运行JVM的CMake测试:

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_minimum_required (VERSION 2.8)

find_package(Java REQUIRED)
include(UseJava)

enable_testing()

project (HelloWorld)

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)

CMake变量 CMAKE_JAVA_COMPILE_FLAGS 可用于指定编译标志。作为副作用, add_jar 命令将设置目标属性 JAR_FILE CLASSDIR 可以分别用于获取生成的jar文件和编译的类文件目录的路径。

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编译java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆