FindJava

尋找 Java

此模組會尋找是否已安裝 Java,並決定標頭檔和程式庫的位置。呼叫者可以設定變數 JAVA_HOME 來明確指定 Java 安裝前綴。

另請參閱 FindJNI 模組以尋找 Java 原生介面 (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)