Java Language


The Java Command - 'java' and 'javaw' All Versions

Java SE 1.0
Java SE 1.1
Java SE 1.2
Java SE 1.3
Java SE 1.4
Java SE 5
Java SE 6
Java SE 7
Java SE 8
Java SE 9 (Early Access)

This draft deletes the entire topic.

Introduction

Introduction

expand all collapse all

Examples

  • 2

    A Java entry-point class has a main method with the following signature and modifiers:

    public static void main(String[] args)
    

    Sidenote: because of how arrays work, it can also be (String args[])

    When the java command starts the virtual machine, it loads the specified entry-point classes and tries to find main. If successful, the arguments from command line are converted to Java String objects and assembled into an array. If main is invoked like this, the array will not be null and won't contain any null entries.

    A valid entry-point class method must do the following:

    • Be named main (case-sensitive)
    • Be public and static
    • Have a void return type
    • Have a single argument with an array String[]. The argument must be present and no more than one argument is allowed.
    • Be generic: type parameters are not allowed.
    • Have a non-generic, top-level (not nested or inner) enclosing class

    It is conventional to declare the class as public but this not strictly necessary. From Java 5 onward, the main method's argument type may be a String varargs instead of a string array. main can optionally throw exceptions, and its parameter can be named anything, but conventionally it is args.

    JavaFX entry-points

    From Java 8 onwards the java command can also directly launch a JavaFX application. JavaFX is documented in the JavaFX tag, but a JavaFX entry-point must do the following:

    • Extend javafx.application.Application
    • Be public and not abstract
    • Not be generic or nested
    • Have an explicit or implicit public no-args constructor
  • 0

    The java command supports a wide range of options:

    • All options start with a single hyphen or minus-sign (-): the GNU/Linux convention of using -- for "long" options is not supported.

    • Options must appear before the <classname> or the -jar <jarfile> argument to be recognized. Any arguments after them will be treated as arguments to be passed to Java app that is being run.

    • Options that do not start with -X or -XX are standard options. You can rely on all Java implementations1 to support any standard option.

    • Options that start with -X are non-standard options, and may be withdrawn from one Java version to the next.

    • Options that start with -XX are advanced options, and may also be withdrawn.

    Setting system properties with -D

    The -D<property>=<value> option is used to set a property in the system Properties object. This parameter can be repeated to set different properties.

    Memory, Stack and Garbage Collector options

    The main options for controlling the heap and stack sizes are documented in Setting the Heap, PermGen and Stack sizes. (Editorial note: Garbage Collector options should be described in the same topic.)

    Enabling and disabling assertions

    The -ea and -da options respectively enable and disable Java assert checking:

    • All assertion checking is disabled by default.
    • The -ea option enables checking of all assertions
    • The -ea:<packagename>... enables checking of assertions in a package and all subpackages.
    • The -ea:<classname>... enables checking of assertions in a class.
    • The -da option disables checking of all assertions
    • The -da:<packagename>... disables checking of assertions in a package and all subpackages.
    • The -da:<classname>... disables checking of assertions in a class.
    • The -esa option enables checking for all system classes.
    • The -dsa option disables checking for all system classes.

    The options can be combined. For example.

    $ # Enable all assertion checking in non-system classes 
    $ java -ea -dsa MyApp
    
    $ # Enable assertions for all classes in a package except for one.
    $ java -ea:com.wombat.fruitbat... -da:com.wombat.fruitbat.Brickbat MyApp
    

    Note that enabling to assertion checking is liable to alter the behavior of a Java programming.

    • It is liable make the application slower in general.
    • It can cause specific methods to take longer to run, which could change timing of threads in a multi-threaded application.
    • It can introduce serendipitous happens-before relations which can cause memory anomalies to disappear.
    • An incorrectly implemented assert statement could have unwanted side-effects.

    Selecting the VM type

    The -client and -server options allow you to select between two different forms of the HotSpot VM:

    • The "client" form is tuned for user applications and offers faster startup.
    • The "server" form is tuned for long running applications. It takes longer capturing statistic during JVM "warm up" which allows the JIT compiler to do a better of job of optimizing the native code.

    By default, the JVM will run in 64bit mode if possible, depending on the capabilities of the platform. The -d32 and -d64 options allow you to select the mode explicitly.


    1 - Check the official manual for the java command. Sometimes a standard option is described as "subject to change".

  • 0

    This is a continuation of the "main class" and "executable JAR" examples.

    Typical Java applications consist of an application-specific code, and various reusable library code that you have implemented or that has been implemented by third parties. The latter are commonly referred to as library dependencies, and are typically packaged as JAR files.

    Java is a dynamically bound language. When you run a Java application with library dependencies, the JVM needs to know where the dependencies are so that it can load classes as required. Broadly speaking, there are two ways to deal with this:

    • The application and its dependencies can be repackaged into a single JAR file that contains all of the required classes and resources.

    • The JVM can be told where to find the dependent JAR files via the runtime classpath.

    For an executable JAR file, the runtime classpath is specified by the "Class-Path" manifest attribute. (Editorial Note: This should be described in a separate Topic on the jar command.) Otherwise, the runtime classpath needs to be supplied using the -cp option or using the CLASSPATH environment variable.

    For example, suppose that we have a Java application in the "myApp.jar" file whose entry point class is com.example.MyApp. Suppose also that the application depends on library JAR files "lib/library1.jar" and "lib/library2.jar". We could launch the application using the java command as follows in a command line:

    $ # Alternative 1 (preferred)
    $ java -cp myApp.jar:lib/library1.jar:lib/library2.jar com.example.MyApp
    
    $ # Alternative 2
    $ export CLASSPATH=myApp.jar:lib/library1.jar:lib/library2.jar
    $ java com.example.MyApp
    

    (On Windows, you would use ; instead of : as the classpath separator, and you would set the (local) CLASSPATH variable using set rather than export.)

    While a Java developer would be comfortable with that, it is not "user friendly". So it is common practice to write a simple shell script (or Windows batch file) to hide the details that the user doesn't need to know about. For example, if you put the following shell script into a file called "myApp", made it executable, and put it into a directory on the command search path:

    #!/bin/bash
    # The 'myApp' wrapper script
    
    export DIR=/usr/libexec/myApp
    export CLASSPATH=$DIR/myApp.jar:$DIR/lib/library1.jar:$DIR/lib/library2.jar
    java com.example.MyApp
    

    then you could run it as follows:

    $ myApp arg1 arg2 ...
    

    Any arguments on the command line will be passed to the Java application via the "$@" expansion. (You can do something similar with a Windows batch file, though the syntax is different.)

Please consider making a request to improve this example.

Syntax

  • java [ <opt> ... ] <class-name> [ <argument> ... ]

  • java [ <opt> ... ] -jar <jar-file-pathname> [ <argument> ... ]

Parameters

Parameters

Remarks

The java command is used for running a Java application from the command line. It is available as a part of any Java SE JRE or JDK.

On Windows systems there are two variants of the java command:

  • The java variant launches the application in a new console window.
  • The javaw variant launches the application without creating a new console window.

On other systems (e.g. Linux, Mac OSX, UNIX) only the java command is provided, and it does not launch a new console window.

The <opt> symbol in the syntax denotes an option on the java command line. The "Java Options" and "Heap and stack sizing options" topics cover the most commonly used options. Others are covered in the JVM Flags topic.

Still have a question about The Java Command - 'java' and 'javaw'? Ask Question

Topic Outline