UseJava

From Get docs
Cmake/docs/3.21/module/usejava


UseJava

This file provides support for Java. It is assumed that FindJava has already been loaded. See FindJava for information on how to load Java into your CMake project.

Synopsis

Creating and Installing JARS
  add_jar (<target_name> [SOURCES] <source1> [<source2>...] ...)
  install_jar (<target_name> DESTINATION <destination> [COMPONENT <component>])
  install_jni_symlink (<target_name> DESTINATION <destination> [COMPONENT <component>])

Header Generation
  create_javah ((TARGET <target> | GENERATED_FILES <VAR>) CLASSES <class>... ...)

Exporting JAR Targets
  install_jar_exports (TARGETS <jars>... FILE <filename> DESTINATION <destination> ...)
  export_jars (TARGETS <jars>... [NAMESPACE <namespace>] FILE <filename>)

Finding JARs
  find_jar (<VAR> NAMES <name1> [<name2>...] [PATHS <path1> [<path2>... ENV <var>]] ...)

Creating Java Documentation
  create_javadoc (<VAR> (PACKAGES <pkg1> [<pkg2>...] | FILES <file1> [<file2>...]) ...)

Creating And Installing JARs

add_jar

Creates a jar file containing java objects and, optionally, resources:

add_jar(<target_name>
        [SOURCES] <source1> [<source2>...] [<resource1>...]
        [RESOURCES NAMESPACE <ns1> <resource1>... [NAMESPACE <nsX> <resourceX>...]... ]
        [INCLUDE_JARS <jar1> [<jar2>...]]
        [ENTRY_POINT <entry>]
        [VERSION <version>]
        [MANIFEST <manifest>]
        [OUTPUT_NAME <name>]
        [OUTPUT_DIR <dir>]
        [GENERATE_NATIVE_HEADERS <target>
                                 [DESTINATION (<dir>|INSTALL <dir> [BUILD <dir>])]]
        )

This command creates a <target_name>.jar. It compiles the given <source> files and adds the given <resource> files to the jar file. Source files can be java files or listing files (prefixed by @). If only resource files are given then just a jar file is created.

SOURCES

Compiles the specified source files and adds the result in the jar file.

New in version 3.4: Support for response files, prefixed by @.

RESOURCES

New in version 3.21.

Adds the named <resource> files to the jar by stripping the source file path and placing the file beneath <ns> within the jar.

For example:

RESOURCES NAMESPACE "/com/my/namespace" "a/path/to/resource.txt"

results in a resource accessible via /com/my/namespace/resource.txt within the jar.

Resources may be added without adjusting the namespace by adding them to the list of SOURCES (original behavior), in this case, resource paths must be relative to CMAKE_CURRENT_SOURCE_DIR. Adding resources without using the RESOURCES parameter in out of source builds will almost certainly result in confusion.

Note

Adding resources via the SOURCES parameter relies upon a hard-coded list of file extensions which are tested to determine whether they compile (e.g. File.java). SOURCES files which match the extensions are compiled. Files which do not match are treated as resources. To include uncompiled resources matching those file extensions use the RESOURCES parameter.

INCLUDE_JARS

The list of jars are added to the classpath when compiling the java sources and also to the dependencies of the target. INCLUDE_JARS also accepts other target names created by add_jar(). For backwards compatibility, jar files listed as sources are ignored (as they have been since the first version of this module).

ENTRY_POINT

Defines an entry point in the jar file.

VERSION

Adds a version to the target output name.

The following example will create a jar file with the name shibboleet-1.2.0.jar and will create a symlink shibboleet.jar pointing to the jar with the version information.

add_jar(shibboleet shibbotleet.java VERSION 1.2.0)
MANIFEST

Defines a custom manifest for the jar.

OUTPUT_NAME

Specify a different output name for the target.

OUTPUT_DIR

Sets the directory where the jar file will be generated. If not specified, CMAKE_CURRENT_BINARY_DIR is used as the output directory.

GENERATE_NATIVE_HEADERS

New in version 3.11.

Generates native header files for methods declared as native. These files provide the connective glue that allow your Java and C code to interact. An INTERFACE target will be created for an easy usage of generated files. Sub-option DESTINATION can be used to specify the output directory for generated header files.

This option requires, at least, version 1.8 of the JDK.

For an optimum usage of this option, it is recommended to include module JNI before any call to add_jar(). The produced target for native headers can then be used to compile C/C++ sources with the target_link_libraries() command.

find_package(JNI)
add_jar(foo foo.java GENERATE_NATIVE_HEADERS foo-native)
add_library(bar bar.cpp)
target_link_libraries(bar PRIVATE foo-native)

New in version 3.20: DESTINATION sub-option now supports the possibility to specify different output directories for BUILD and INSTALL steps. If BUILD directory is not specified, a default directory will be used.

To export the interface target generated by GENERATE_NATIVE_HEADERS option, sub-option INSTALL of DESTINATION is required:

add_jar(foo foo.java GENERATE_NATIVE_HEADERS foo-native
                     DESTINATION INSTALL include)
install(TARGETS foo-native EXPORT native)
install(DIRECTORY "$<TARGET_PROPERTY:foo-native,NATIVE_HEADERS_DIRECTORY>/"
        DESTINATION include)
install(EXPORT native DESTINATION /to/export NAMESPACE foo)

Some variables can be set to customize the behavior of add_jar() as well as the java compiler:

CMAKE_JAVA_COMPILE_FLAGS

Specify additional flags to java compiler.

CMAKE_JAVA_INCLUDE_PATH

Specify additional paths to the class path.

CMAKE_JNI_TARGET

If the target is a JNI library, sets this boolean variable to TRUE to enable creation of a JNI symbolic link (see also install_jni_symlink()).

CMAKE_JAR_CLASSES_PREFIX

If multiple jars should be produced from the same java source filetree, to prevent the accumulation of duplicate class files in subsequent jars, set/reset CMAKE_JAR_CLASSES_PREFIX prior to calling the add_jar():

set(CMAKE_JAR_CLASSES_PREFIX com/redhat/foo)
add_jar(foo foo.java)

set(CMAKE_JAR_CLASSES_PREFIX com/redhat/bar)
add_jar(bar bar.java)

The add_jar() function sets the following target properties on <target_name>:

INSTALL_FILES

The files which should be installed. This is used by install_jar().

JNI_SYMLINK

The JNI symlink which should be installed. This is used by install_jni_symlink().

JAR_FILE

The location of the jar file so that you can include it.

CLASSDIR

The directory where the class files can be found. For example to use them with javah.

NATIVE_HEADERS_DIRECTORY

New in version 3.20.

The directory where native headers are generated. Defined when option GENERATE_NATIVE_HEADERS is specified.

install_jar

This command installs the jar file to the given destination:

install_jar(<target_name> <destination>)
install_jar(<target_name> DESTINATION <destination> [COMPONENT <component>])

This command installs the <target_name> file to the given <destination>. It should be called in the same scope as add_jar() or it will fail.

New in version 3.4: The second signature with DESTINATION and COMPONENT options.

DESTINATION

Specify the directory on disk to which a file will be installed.

COMPONENT

Specify an installation component name with which the install rule is associated, such as "runtime" or "development".

The install_jar() command sets the following target properties on <target_name>:

INSTALL_DESTINATION

Holds the <destination> as described above, and is used by install_jar_exports().

install_jni_symlink

Installs JNI symlinks for target generated by add_jar():

install_jni_symlink(<target_name> <destination>)
install_jni_symlink(<target_name> DESTINATION <destination> [COMPONENT <component>])

This command installs the <target_name> JNI symlinks to the given <destination>. It should be called in the same scope as add_jar() or it will fail.

New in version 3.4: The second signature with DESTINATION and COMPONENT options.

DESTINATION

Specify the directory on disk to which a file will be installed.

COMPONENT

Specify an installation component name with which the install rule is associated, such as "runtime" or "development".

Utilize the following commands to create a JNI symbolic link:

set(CMAKE_JNI_TARGET TRUE)
add_jar(shibboleet shibbotleet.java VERSION 1.2.0)
install_jar(shibboleet ${LIB_INSTALL_DIR}/shibboleet)
install_jni_symlink(shibboleet ${JAVA_LIB_INSTALL_DIR})

Header Generation

create_javah

New in version 3.4.

Generates C header files for java classes:

create_javah(TARGET <target> | GENERATED_FILES <VAR>
             CLASSES <class>...
             [CLASSPATH <classpath>...]
             [DEPENDS <depend>...]
             [OUTPUT_NAME <path>|OUTPUT_DIR <path>]
             )

Deprecated since version 3.11: This command will no longer be supported starting with version 10 of the JDK due to the suppression of javah tool. The add_jar(GENERATE_NATIVE_HEADERS) command should be used instead.

Create C header files from java classes. These files provide the connective glue that allow your Java and C code to interact.

There are two main signatures for create_javah(). The first signature returns generated files through variable specified by the GENERATED_FILES option. For example:

create_javah(GENERATED_FILES files_headers
  CLASSES org.cmake.HelloWorld
  CLASSPATH hello.jar
)

The second signature for create_javah() creates a target which encapsulates header files generation. E.g.

create_javah(TARGET target_headers
  CLASSES org.cmake.HelloWorld
  CLASSPATH hello.jar
)

Both signatures share same options.

CLASSES

Specifies Java classes used to generate headers.

CLASSPATH

Specifies various paths to look up classes. Here .class files, jar files or targets created by command add_jar can be used.

DEPENDS

Targets on which the javah target depends.

OUTPUT_NAME

Concatenates the resulting header files for all the classes listed by option CLASSES into <path>. Same behavior as option -o of javah tool.

OUTPUT_DIR

Sets the directory where the header files will be generated. Same behavior as option -d of javah tool. If not specified, CMAKE_CURRENT_BINARY_DIR is used as the output directory.

Exporting JAR Targets

install_jar_exports

New in version 3.7.

Installs a target export file:

install_jar_exports(TARGETS <jars>...
                    [NAMESPACE <namespace>]
                    FILE <filename>
                    DESTINATION <destination> [COMPONENT <component>])

This command installs a target export file <filename> for the named jar targets to the given <destination> directory. Its function is similar to that of install(EXPORT).

TARGETS

List of targets created by add_jar() command.

NAMESPACE

New in version 3.9.

The <namespace> value will be prepend to the target names as they are written to the import file.

FILE

Specify name of the export file.

DESTINATION

Specify the directory on disk to which a file will be installed.

COMPONENT

Specify an installation component name with which the install rule is associated, such as "runtime" or "development".

export_jars

New in version 3.7.

Writes a target export file:

export_jars(TARGETS <jars>...
            [NAMESPACE <namespace>]
            FILE <filename>)

This command writes a target export file <filename> for the named <jars> targets. Its function is similar to that of export().

TARGETS

List of targets created by add_jar() command.

NAMESPACE

New in version 3.9.

The <namespace> value will be prepend to the target names as they are written to the import file.

FILE

Specify name of the export file.

Finding JARs

find_jar

Finds the specified jar file:

find_jar(<VAR>
         <name> | NAMES <name1> [<name2>...]
         [PATHS <path1> [<path2>... ENV <var>]]
         [VERSIONS <version1> [<version2>]]
         [DOC "cache documentation string"]
        )

This command is used to find a full path to the named jar. A cache entry named by <VAR> is created to store the result of this command. If the full path to a jar is found the result is stored in the variable and the search will not repeated unless the variable is cleared. If nothing is found, the result will be <VAR>-NOTFOUND, and the search will be attempted again next time find_jar() is invoked with the same variable.

NAMES

Specify one or more possible names for the jar file.

PATHS

Specify directories to search in addition to the default locations. The ENV var sub-option reads paths from a system environment variable.

VERSIONS

Specify jar versions.

DOC

Specify the documentation string for the <VAR> cache entry.

Creating Java Documentation

create_javadoc

Creates java documentation based on files and packages:

create_javadoc(<VAR>
               (PACKAGES <pkg1> [<pkg2>...] | FILES <file1> [<file2>...])
               [SOURCEPATH <sourcepath>]
               [CLASSPATH <classpath>]
               [INSTALLPATH <install path>]
               [DOCTITLE <the documentation title>]
               [WINDOWTITLE <the title of the document>]
               [AUTHOR (TRUE|FALSE)]
               [USE (TRUE|FALSE)]
               [VERSION (TRUE|FALSE)]
               )

The create_javadoc() command can be used to create java documentation. There are two main signatures for create_javadoc().

The first signature works with package names on a path with source files:

create_javadoc(my_example_doc
               PACKAGES com.example.foo com.example.bar
               SOURCEPATH "${CMAKE_CURRENT_SOURCE_DIR}"
               CLASSPATH ${CMAKE_JAVA_INCLUDE_PATH}
               WINDOWTITLE "My example"
               DOCTITLE "<h1>My example</h1>"
               AUTHOR TRUE
               USE TRUE
               VERSION TRUE
              )

The second signature for create_javadoc() works on a given list of files:

create_javadoc(my_example_doc
               FILES java/A.java java/B.java
               CLASSPATH ${CMAKE_JAVA_INCLUDE_PATH}
               WINDOWTITLE "My example"
               DOCTITLE "<h1>My example</h1>"
               AUTHOR TRUE
               USE TRUE
               VERSION TRUE
              )

Both signatures share most of the options. For more details please read the javadoc manpage.

PACKAGES

Specify java packages.

FILES

Specify java source files. If relative paths are specified, they are relative to CMAKE_CURRENT_SOURCE_DIR.

SOURCEPATH

Specify the directory where to look for packages. By default, CMAKE_CURRENT_SOURCE_DIR directory is used.

CLASSPATH

Specify where to find user class files. Same behavior as option -classpath of javadoc tool.

INSTALLPATH

Specify where to install the java documentation. If you specified, the documentation will be installed to ${CMAKE_INSTALL_PREFIX}/share/javadoc/<VAR>.

DOCTITLE

Specify the title to place near the top of the overview summary file. Same behavior as option -doctitle of javadoc tool.

WINDOWTITLE

Specify the title to be placed in the HTML <title> tag. Same behavior as option -windowtitle of javadoc tool.

AUTHOR

When value TRUE is specified, includes the @author text in the generated docs. Same behavior as option -author of javadoc tool.

USE

When value TRUE is specified, creates class and package usage pages. Includes one Use page for each documented class and package. Same behavior as option -use of javadoc tool.

VERSION

When value TRUE is specified, includes the version text in the generated docs. Same behavior as option -version of javadoc tool.

© 2000–2021 Kitware, Inc. and Contributors
Licensed under the BSD 3-clause License.
https://cmake.org/cmake/help/v3.21/module/UseJava.html