FindJava

查找 Java

此模块可查找是否安装了 Java,并确定包含文件和库所在的位置。调用方可以显式设置变量 JAVA_HOME 以指定 Java 安装前缀。

另请参阅 FindJNI 模块,它可查找 Java Native Interface (JNI)。

3.10 版本中添加:增加了对 Java 9+ 版本解析的支持。

在调用此查找模块时指定以下组件中的一个或多个。请参阅下面的示例。

Runtime     = Java Runtime Environment used to execute Java byte-compiled applications
Development = Development tools (java, javac, javah, jar and javadoc), includes Runtime component
IdlJ        = Interface Description Language (IDL) to Java compiler
JarSigner   = Signer and verifier tool for Java Archive (JAR) files

此模块设置了下列结果变量

Java_JAVA_EXECUTABLE      = the full path to the Java runtime
Java_JAVAC_EXECUTABLE     = the full path to the Java compiler
Java_JAVAH_EXECUTABLE     = the full path to the Java header generator
Java_JAVADOC_EXECUTABLE   = the full path to the Java documentation generator
Java_IDLJ_EXECUTABLE      = the full path to the Java idl compiler
Java_JAR_EXECUTABLE       = the full path to the Java archiver
Java_JARSIGNER_EXECUTABLE = the full path to the Java jar signer
Java_VERSION_STRING       = Version of java found, eg. 1.6.0_12
Java_VERSION_MAJOR        = The major version of the package found.
Java_VERSION_MINOR        = The minor version of the package found.
Java_VERSION_PATCH        = The patch version of the package found.
Java_VERSION_TWEAK        = The tweak version of the package found (after '_')
Java_VERSION              = This is set to: $major[.$minor[.$patch[.$tweak]]]

3.4 版本中添加:添加了 Java_IDLJ_EXECUTABLEJava_JARSIGNER_EXECUTABLE 变量。

使用 find_package() 语法可以指定 Java 所需的最低版本,如:

find_package(Java 1.8)

注意: ${Java_VERSION}${Java_VERSION_STRING} 不一定相同。例如,某些 Java 版本可能会返回: Java_VERSION_STRING = 1.8.0_17Java_VERSION = 1.8.0.17

另一个示例是 Java OEM,其中: Java_VERSION_STRING = 1.8.0-oemJava_VERSION = 1.8.0

针对这些组件,将设置以下变量

Java_FOUND                    - TRUE if all components are found.
Java_<component>_FOUND        - TRUE if <component> is found.

示例用法

find_package(Java)
find_package(Java 1.8 REQUIRED)
find_package(Java COMPONENTS Runtime)
find_package(Java COMPONENTS Development)