java - Program Launching Command and Options

This section describes the Java launching tool 'java' and its commonly used command options.

What Is "java"? - "java" is a command line tool that launches Java applications. It starts a Java Virtual Machine (JVM), loads the specified class, and invokes that class's main method.

"java" has been included in JDK installation since JDK 1.0. And it is represented by the %java_home%\bin\java.exe program file.

"java" command has the following syntax:

java [options] class [arguments]

where "options" is a list of options; "class" is the full name of a Java class to be launched; "arguments" is a list of arguments to be passed to the main method of the class to be launched.

Another way of launching a Java class is to use the "-jar" option:

java [options] -jar file [arguments]

where "file" is a JAR file, which should contain a "Main-Class" attribute in the manifest file. The "Main-Class" attribute defines the Java class to be launched.

Commonly used "java" options are:

You can run "java" by typing in "java" at the command prompt, if you have %java_home%\bin directory included in "path" the environment variable. If you run the "java" command without any options, you will get the quick usage information as shown below:

herong> java

Usage: java [options] <mainclass> [args...]
           (to execute a class)
   or  java [options] -jar <jarfile> [args...]
           (to execute a jar file)
   or  java [options] -m <module>[/<mainclass>] [args...]
       java [options] --module <module>[/<mainclass>] [args...]
           (to execute the main class in a module)

 Arguments following the main class, -jar <jarfile>, -m or --module
 <module>/<mainclass> are passed as the arguments to main class.

 where options include:

    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
    --class-path <class search path of directories and zip/jar files>
                  A ; separated list of directories, JAR archives,
                  and ZIP archives to search for class files.
    -p <module path>
    --module-path <module path>...
                  A ; separated list of directories, each directory
                  is a directory of modules.
    --upgrade-module-path <module path>...
                  A ; separated list of directories, each directory
                  is a directory of modules that replace upgradeable
                  modules in the runtime image
    --add-modules <module name>[,<module name>...]
                  root modules to resolve in addition to the initial
                  module. <module name> can also be ALL-DEFAULT,
                  ALL-SYSTEM, ALL-MODULE-PATH.
    --list-modules
                  list observable modules and exit
    -d <module name>
    --describe-module <module name>
                  describe a module and exit
    --dry-run     create VM and load main class but do not execute
                  main method. The --dry-run option may be useful for
                  validating the command-line options such as the module
                  system configuration.
    --validate-modules
                  validate all modules and exit
                  The --validate-modules option may be useful for
                  finding conflicts and other errors with modules on
                  the module path.
    -D<name>=<value>
                  set a system property
    -verbose:[class|module|gc|jni]
                  enable verbose output
    -version      print product version to the error stream and exit
    --version     print product version to the output stream and exit
    -showversion  print product version to the error stream and continue
    --show-version
                  print product version to the output stream and continue
    --show-module-resolution
                  show module resolution output during startup
    -? -h -help
                  print this help message to the error stream
    --help        print this help message to the output stream
    -X            print help on extra options to the error stream
    --help-extra  print help on extra options to the output stream
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
                  enable assertions with specified granularity
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
                  disable assertions with specified granularity
    -esa | -enablesystemassertions
                  enable system assertions
    -dsa | -disablesystemassertions
                  disable system assertions
    -agentlib:<libname>[=<options>]
                  load native agent library <libname>, e.g.
                  -agentlib:jdwp, see also -agentlib:jdwp=help
    -agentpath:<pathname>[=<options>]
                  load native agent library by full pathname
    -javaagent:<jarpath>[=<options>]
                  load Java programming language agent, see
                  java.lang.instrument

    -splash:<imagepath>
                  show splash screen with specified image
                  HiDPI scaled images are automatically supported and
                  used if available. The unscaled image filename, e.g.
                  image.ext, should always be passed as the argument to
                  the -splash option.

                  The most appropriate scaled image provided will be
                  picked up automatically. See the SplashScreen API
                  documentation for more information
    @argument files
                  one or more argument files containing options
    -disable-@files
                  prevent further argument file expansion

To specify an argument for a long option, you can use --<name>=<value>
or --<name> <value>.

For more information, see "java" reference page at https://docs.oracle.com/en/java/javase/17/docs/specs/man/java.html.

Table of Contents

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

 javac - The Java Program Compiler

java - The Java Program Launcher

java - Program Launching Command and Options

 Launching Hello.java - My First Java Program

 "java -classpath" - Specifying Class Path

 "java -jar" - Specifying Executable JAR File

 "java -X" - Specifying Non-Standard Options

 "java --list-modules" - Listing Modules in JDK

 "java --describe-module" - Printing Module Definition

 "java --module" - Launching Program from Module

 "java --module" - Launching Program from Module JAR

 javaw - Launching Java Programs without Console

 jar - The JAR File Tool

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

 jpackage - Binary Package Builder

 javadoc - The Java Document Generator

 jdeps - The Java Class Dependency Analyzer

 jdeprscan - The Java Deprecated API Scanner

 jdb - The Java Debugger

 jcmd - The JVM Diagnostic Tool

 jconsole - Java Monitoring and Management Console

 jstat - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jhsdb - The Java HotSpot Debugger

 jvisualvm (Java VisualVM) - JVM Visual Tool

 jmc - Java Mission Control

 javap - The Java Class File Disassembler

 keytool - Public Key Certificate Tool

 jarsigner - JAR File Signer

 jshell - Java Language Shell

 jrunscript - Script Code Shell

 Miscellaneous Tools

 native2ascii - Native-to-ASCII Encoding Converter

 JAB (Java Access Bridge) for Windows

 Archived Tutorials

 References

 Full Version in PDF/EPUB