FindJava

尋找 Java

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

另請參閱 FindJNI 模組,以尋找 Java Native Interface (JNI)。

Added in version 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]]]

Added in version 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)