Command-Line Options in Java 9+
Command-line options in Java 9 and later versions are essential tools for configuring the Java Virtual Machine (JVM) and the Java compiler. These options provide flexibility and control over the execution and compilation processes, especially when dealing with compatibility issues during migration to newer Java versions.
Overview
Command-line options can be applied in several ways:
-
Directly on the Command Line: This is the most straightforward method, where options are specified directly in the command used to run or compile Java applications.
-
Manifest Entries in Executable JARs: Options can be specified in the manifest file of an executable JAR, allowing them to be automatically applied when the JAR is executed.
-
Environment Variables: The
JDK_JAVA_OPTIONSenvironment variable can be set to include command-line options that should be applied every time the JVM is started. -
Argument Files: Options can be placed in a file, known as an argument file, which is then referenced on the command line. This method is particularly useful for managing a large number of options or for reusing the same set of options across different commands.
Using Argument Files
Argument files simplify the management of command-line options by allowing you to store them in a separate file. This is beneficial for both compilation and execution processes. By referencing an argument file on the command line, you can easily apply a consistent set of options without having to type them out each time.
Examples of Command-Line Options
Compilation Example
When compiling code for Java 9+, you might use options like --add-modules and --patch-module. These options help in making the compilation process compatible with the module system introduced in Java 9. For instance, you can specify these options in an argument file to streamline the compilation process.
Launching an Application
When launching an application from an image, command-line options such as --module can be used to specify the initial module. Additionally, the --module-path option allows you to add new modules, while the --show-module-resolution option can be used to observe the module resolution process. However, it's important to note that system modules included in the image will shadow those specified on the module path.
Example Listing
An example of using command-line options when launching an application image is shown in Listing 14.5, where the --module-path and --show-module-resolution options are used to add a new module and observe module resolution.
Figure 14.5: Example of using command-line options when launching an application image.
By understanding and utilizing these command-line options, developers can effectively manage the behavior of their Java applications and ensure compatibility with the evolving Java platform.
FAQ (Frequently asked questions)
What are command-line options used for in Java 9+?
How can command-line options be applied in Java 9+?
Why are command-line options important during migration?
How can command-line options help in compiling code for Java 9+?
What is an argument file in the context of command-line options?
What are the four ways to apply command-line options in Java?
How can argument files be used with command-line options?
Why are argument files useful in Java 9+?
What command-line options are used in Listing 14.5 for launching an application image?
What is the purpose of the '--module-path' option?
What does the '--show-module-resolution' option do?
How can you specify the initial module when launching an application from an image?
What happens to system modules in an image when using the module path?