1# Distributed under the OSI-approved BSD 3-Clause License. See accompanying 2# file Copyright.txt or https://cmake.org/licensing for details. 3 4#[=======================================================================[.rst: 5FindJava 6-------- 7 8Find Java 9 10This module finds if Java is installed and determines where the 11include files and libraries are. The caller may set variable ``JAVA_HOME`` 12to specify a Java installation prefix explicitly. 13 14See also the :module:`FindJNI` module to find Java Native Interface (JNI). 15 16Specify one or more of the following components as you call this find module. See example below. 17 18:: 19 20 Runtime = Java Runtime Environment used to execute Java byte-compiled applications 21 Development = Development tools (java, javac, javah, jar and javadoc), includes Runtime component 22 IdlJ = Interface Description Language (IDL) to Java compiler 23 JarSigner = Signer and verifier tool for Java Archive (JAR) files 24 25 26This module sets the following result variables: 27 28:: 29 30 Java_JAVA_EXECUTABLE = the full path to the Java runtime 31 Java_JAVAC_EXECUTABLE = the full path to the Java compiler 32 Java_JAVAH_EXECUTABLE = the full path to the Java header generator 33 Java_JAVADOC_EXECUTABLE = the full path to the Java documentation generator 34 Java_IDLJ_EXECUTABLE = the full path to the Java idl compiler 35 Java_JAR_EXECUTABLE = the full path to the Java archiver 36 Java_JARSIGNER_EXECUTABLE = the full path to the Java jar signer 37 Java_VERSION_STRING = Version of java found, eg. 1.6.0_12 38 Java_VERSION_MAJOR = The major version of the package found. 39 Java_VERSION_MINOR = The minor version of the package found. 40 Java_VERSION_PATCH = The patch version of the package found. 41 Java_VERSION_TWEAK = The tweak version of the package found (after '_') 42 Java_VERSION = This is set to: $major[.$minor[.$patch[.$tweak]]] 43 44 45 46The minimum required version of Java can be specified using the 47:command:`find_package` syntax, e.g. 48 49.. code-block:: cmake 50 51 find_package(Java 1.8) 52 53NOTE: ``${Java_VERSION}`` and ``${Java_VERSION_STRING}`` are not guaranteed to 54be identical. For example some java version may return: 55``Java_VERSION_STRING = 1.8.0_17`` and ``Java_VERSION = 1.8.0.17`` 56 57another example is the Java OEM, with: ``Java_VERSION_STRING = 1.8.0-oem`` 58and ``Java_VERSION = 1.8.0`` 59 60For these components the following variables are set: 61 62:: 63 64 Java_FOUND - TRUE if all components are found. 65 Java_<component>_FOUND - TRUE if <component> is found. 66 67 68 69Example Usages: 70 71:: 72 73 find_package(Java) 74 find_package(Java 1.8 REQUIRED) 75 find_package(Java COMPONENTS Runtime) 76 find_package(Java COMPONENTS Development) 77#]=======================================================================] 78 79include(CMakeFindJavaCommon) 80 81# The HINTS option should only be used for values computed from the system. 82set(_JAVA_HINTS) 83if(_JAVA_HOME) 84 list(APPEND _JAVA_HINTS ${_JAVA_HOME}/bin) 85endif() 86if (WIN32) 87 macro (_JAVA_GET_INSTALLED_VERSIONS _KIND) 88 execute_process(COMMAND REG QUERY HKLM\\SOFTWARE\\JavaSoft\\${_KIND} 89 RESULT_VARIABLE _JAVA_RESULT 90 OUTPUT_VARIABLE _JAVA_VERSIONS 91 ERROR_QUIET) 92 if (NOT _JAVA_RESULT) 93 string (REGEX MATCHALL "HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\JavaSoft\\\\${_KIND}\\\\[0-9.]+" _JAVA_VERSIONS "${_JAVA_VERSIONS}") 94 if (_JAVA_VERSIONS) 95 # sort versions. Most recent first 96 ## handle version 9 apart from other versions to get correct ordering 97 set (_JAVA_V9 ${_JAVA_VERSIONS}) 98 list (FILTER _JAVA_VERSIONS EXCLUDE REGEX "${_KIND}\\\\9") 99 list (SORT _JAVA_VERSIONS) 100 list (REVERSE _JAVA_VERSIONS) 101 list (FILTER _JAVA_V9 INCLUDE REGEX "${_KIND}\\\\9") 102 list (SORT _JAVA_V9) 103 list (REVERSE _JAVA_V9) 104 list (APPEND _JAVA_VERSIONS ${_JAVA_V9}) 105 foreach (_JAVA_HINT IN LISTS _JAVA_VERSIONS) 106 list(APPEND _JAVA_HINTS "[${_JAVA_HINT};JavaHome]/bin") 107 endforeach() 108 endif() 109 endif() 110 endmacro() 111 112 # search for installed versions for version 9 and upper 113 _JAVA_GET_INSTALLED_VERSIONS("JDK") 114 _JAVA_GET_INSTALLED_VERSIONS("JRE") 115 116 list(APPEND _JAVA_HINTS 117 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.9;JavaHome]/bin" 118 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.8;JavaHome]/bin" 119 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.7;JavaHome]/bin" 120 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.6;JavaHome]/bin" 121 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.5;JavaHome]/bin" 122 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.4;JavaHome]/bin" 123 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.3;JavaHome]/bin" 124 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.9;JavaHome]/bin" 125 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.8;JavaHome]/bin" 126 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.7;JavaHome]/bin" 127 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.6;JavaHome]/bin" 128 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.5;JavaHome]/bin" 129 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.4;JavaHome]/bin" 130 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.3;JavaHome]/bin" 131 ) 132endif() 133 134# Hard-coded guesses should still go in PATHS. This ensures that the user 135# environment can always override hard guesses. 136set(_JAVA_PATHS 137 /usr/lib/java/bin 138 /usr/share/java/bin 139 /usr/local/java/bin 140 /usr/local/java/share/bin 141 /usr/java/j2sdk1.4.2_04 142 /usr/lib/j2sdk1.4-sun/bin 143 /usr/java/j2sdk1.4.2_09/bin 144 /usr/lib/j2sdk1.5-sun/bin 145 /opt/sun-jdk-1.5.0.04/bin 146 /usr/local/jdk-1.7.0/bin 147 /usr/local/jdk-1.6.0/bin 148 ) 149find_program(Java_JAVA_EXECUTABLE 150 NAMES java 151 HINTS ${_JAVA_HINTS} 152 PATHS ${_JAVA_PATHS} 153) 154 155if(Java_JAVA_EXECUTABLE) 156 execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -version 157 RESULT_VARIABLE res 158 OUTPUT_VARIABLE var 159 ERROR_VARIABLE var # sun-java output to stderr 160 OUTPUT_STRIP_TRAILING_WHITESPACE 161 ERROR_STRIP_TRAILING_WHITESPACE) 162 if( res ) 163 if(var MATCHES "Unable to locate a Java Runtime to invoke|No Java runtime present, requesting install") 164 set(Java_JAVA_EXECUTABLE Java_JAVA_EXECUTABLE-NOTFOUND) 165 elseif(${Java_FIND_REQUIRED}) 166 message( FATAL_ERROR "Error executing java -version" ) 167 else() 168 message( STATUS "Warning, could not run java -version") 169 endif() 170 else() 171 # Extract version components (up to 4 levels) from "java -version" output. 172 set(_java_version_regex [[(([0-9]+)(\.([0-9]+)(\.([0-9]+)(_([0-9]+))?)?)?.*)]]) 173 if(var MATCHES "java version \"${_java_version_regex}\"") 174 # Sun, GCJ, older OpenJDK 175 set(Java_VERSION_STRING "${CMAKE_MATCH_1}") 176 set(Java_VERSION_MAJOR "${CMAKE_MATCH_2}") 177 if (CMAKE_MATCH_4) 178 set(Java_VERSION_MINOR "${CMAKE_MATCH_4}") 179 else() 180 set(Java_VERSION_MINOR 0) 181 endif() 182 if (CMAKE_MATCH_6) 183 set(Java_VERSION_PATCH "${CMAKE_MATCH_6}") 184 else() 185 set(Java_VERSION_PATCH 0) 186 endif() 187 set(Java_VERSION_TWEAK "${CMAKE_MATCH_8}") 188 elseif(var MATCHES "openjdk version \"${_java_version_regex}\"") 189 # OpenJDK 190 set(Java_VERSION_STRING "${CMAKE_MATCH_1}") 191 set(Java_VERSION_MAJOR "${CMAKE_MATCH_2}") 192 if (CMAKE_MATCH_4) 193 set(Java_VERSION_MINOR "${CMAKE_MATCH_4}") 194 else() 195 set(Java_VERSION_MINOR 0) 196 endif() 197 if (CMAKE_MATCH_6) 198 set(Java_VERSION_PATCH "${CMAKE_MATCH_6}") 199 else() 200 set(Java_VERSION_PATCH 0) 201 endif() 202 set(Java_VERSION_TWEAK "${CMAKE_MATCH_8}") 203 elseif(var MATCHES "openjdk version \"([0-9]+)-[A-Za-z]+\"") 204 # OpenJDK 9 early access builds or locally built 205 set(Java_VERSION_STRING "1.${CMAKE_MATCH_1}.0") 206 set(Java_VERSION_MAJOR "1") 207 set(Java_VERSION_MINOR "${CMAKE_MATCH_1}") 208 set(Java_VERSION_PATCH "0") 209 set(Java_VERSION_TWEAK "") 210 elseif(var MATCHES "java full version \"kaffe-${_java_version_regex}\"") 211 # Kaffe style 212 set(Java_VERSION_STRING "${CMAKE_MATCH_1}") 213 set(Java_VERSION_MAJOR "${CMAKE_MATCH_2}") 214 set(Java_VERSION_MINOR "${CMAKE_MATCH_4}") 215 set(Java_VERSION_PATCH "${CMAKE_MATCH_6}") 216 set(Java_VERSION_TWEAK "${CMAKE_MATCH_8}") 217 else() 218 if(NOT Java_FIND_QUIETLY) 219 string(REPLACE "\n" "\n " ver_msg "\n${var}") 220 message(WARNING "Java version not recognized:${ver_msg}\nPlease report.") 221 endif() 222 set(Java_VERSION_STRING "") 223 set(Java_VERSION_MAJOR "") 224 set(Java_VERSION_MINOR "") 225 set(Java_VERSION_PATCH "") 226 set(Java_VERSION_TWEAK "") 227 endif() 228 set(Java_VERSION "${Java_VERSION_MAJOR}") 229 if(NOT "x${Java_VERSION}" STREQUAL "x") 230 foreach(c MINOR PATCH TWEAK) 231 if(NOT "x${Java_VERSION_${c}}" STREQUAL "x") 232 string(APPEND Java_VERSION ".${Java_VERSION_${c}}") 233 else() 234 break() 235 endif() 236 endforeach() 237 endif() 238 endif() 239 240endif() 241 242 243find_program(Java_JAR_EXECUTABLE 244 NAMES jar 245 HINTS ${_JAVA_HINTS} 246 PATHS ${_JAVA_PATHS} 247) 248 249find_program(Java_JAVAC_EXECUTABLE 250 NAMES javac 251 HINTS ${_JAVA_HINTS} 252 PATHS ${_JAVA_PATHS} 253) 254 255find_program(Java_JAVAH_EXECUTABLE 256 NAMES javah 257 HINTS ${_JAVA_HINTS} 258 PATHS ${_JAVA_PATHS} 259) 260 261find_program(Java_JAVADOC_EXECUTABLE 262 NAMES javadoc 263 HINTS ${_JAVA_HINTS} 264 PATHS ${_JAVA_PATHS} 265) 266 267find_program(Java_IDLJ_EXECUTABLE 268 NAMES idlj 269 HINTS ${_JAVA_HINTS} 270 PATHS ${_JAVA_PATHS} 271) 272 273find_program(Java_JARSIGNER_EXECUTABLE 274 NAMES jarsigner 275 HINTS ${_JAVA_HINTS} 276 PATHS ${_JAVA_PATHS} 277) 278 279include(FindPackageHandleStandardArgs) 280if(Java_FIND_COMPONENTS) 281 set(_JAVA_REQUIRED_VARS) 282 foreach(component ${Java_FIND_COMPONENTS}) 283 # User just want to execute some Java byte-compiled 284 If(component STREQUAL "Runtime") 285 list(APPEND _JAVA_REQUIRED_VARS Java_JAVA_EXECUTABLE) 286 if(Java_JAVA_EXECUTABLE) 287 set(Java_Runtime_FOUND TRUE) 288 endif() 289 elseif(component STREQUAL "Development") 290 list(APPEND _JAVA_REQUIRED_VARS Java_JAVA_EXECUTABLE Java_JAVAC_EXECUTABLE 291 Java_JAR_EXECUTABLE Java_JAVADOC_EXECUTABLE) 292 if(Java_VERSION VERSION_LESS "10") 293 list(APPEND _JAVA_REQUIRED_VARS Java_JAVAH_EXECUTABLE) 294 if(Java_JAVA_EXECUTABLE AND Java_JAVAC_EXECUTABLE 295 AND Java_JAVAH_EXECUTABLE AND Java_JAR_EXECUTABLE AND Java_JAVADOC_EXECUTABLE) 296 set(Java_Development_FOUND TRUE) 297 endif() 298 else() 299 if(Java_JAVA_EXECUTABLE AND Java_JAVAC_EXECUTABLE 300 AND Java_JAR_EXECUTABLE AND Java_JAVADOC_EXECUTABLE) 301 set(Java_Development_FOUND TRUE) 302 endif() 303 endif() 304 elseif(component STREQUAL "IdlJ") 305 list(APPEND _JAVA_REQUIRED_VARS Java_IDLJ_EXECUTABLE) 306 if(Java_IDLJ_EXECUTABLE) 307 set(Java_IdlJ_FOUND TRUE) 308 endif() 309 elseif(component STREQUAL "JarSigner") 310 list(APPEND _JAVA_REQUIRED_VARS Java_JARSIGNER_EXECUTABLE) 311 if(Java_JARSIGNER_EXECUTABLE) 312 set(Java_JarSigner_FOUND TRUE) 313 endif() 314 else() 315 message(FATAL_ERROR "Comp: ${component} is not handled") 316 endif() 317 endforeach() 318 list (REMOVE_DUPLICATES _JAVA_REQUIRED_VARS) 319 find_package_handle_standard_args(Java 320 REQUIRED_VARS ${_JAVA_REQUIRED_VARS} HANDLE_COMPONENTS 321 VERSION_VAR Java_VERSION 322 ) 323 if(Java_FOUND) 324 foreach(component ${Java_FIND_COMPONENTS}) 325 set(Java_${component}_FOUND TRUE) 326 endforeach() 327 endif() 328else() 329 # Check for Development 330 if(Java_VERSION VERSION_LESS "10") 331 find_package_handle_standard_args(Java 332 REQUIRED_VARS Java_JAVA_EXECUTABLE Java_JAR_EXECUTABLE Java_JAVAC_EXECUTABLE 333 Java_JAVAH_EXECUTABLE Java_JAVADOC_EXECUTABLE 334 VERSION_VAR Java_VERSION_STRING 335 ) 336 else() 337 find_package_handle_standard_args(Java 338 REQUIRED_VARS Java_JAVA_EXECUTABLE Java_JAR_EXECUTABLE Java_JAVAC_EXECUTABLE 339 Java_JAVADOC_EXECUTABLE 340 VERSION_VAR Java_VERSION_STRING 341 ) 342 endif() 343endif() 344 345 346mark_as_advanced( 347 Java_JAVA_EXECUTABLE 348 Java_JAR_EXECUTABLE 349 Java_JAVAC_EXECUTABLE 350 Java_JAVAH_EXECUTABLE 351 Java_JAVADOC_EXECUTABLE 352 Java_IDLJ_EXECUTABLE 353 Java_JARSIGNER_EXECUTABLE 354 ) 355 356# LEGACY 357set(JAVA_RUNTIME ${Java_JAVA_EXECUTABLE}) 358set(JAVA_ARCHIVE ${Java_JAR_EXECUTABLE}) 359set(JAVA_COMPILE ${Java_JAVAC_EXECUTABLE}) 360