java [ options ] class [ argument ... ] java [ options ] -jar file.jar [ argument ... ] javaw [ options ] class [ argument ... ] javaw [ options ] -jar file.jar [ argument ... ] oldjava [ options ] class [ argument ... ] oldjavaw [ options ] class [ argument ... ]
- Command-line options.
- Name of the class to be invoked.
- Name of the jar file to be invoked. Used only with
- Argument passed to the main function.
The java tool launches a Java application. It does this by starting a Java runtime environment, loading a specified class, and invoking that class's main method. The method declaration must look like the following:public static void main(String args)The method must be declared public and static, it must not return any value, and it must accept a String array as a parameter. By default, the first non-option argument is the name of the class to be invoked. A fully-qualified class name should be used. If the -jar option is specified, the first non-option argument is the name of a JAR archive containing class and resource files for the application, with the startup class indicated by the Main-Class manifest header.
The Java runtime searches for the startup class, and other classes used, in three sets of locations: the bootstrap class path, the installed extensions, and the user class path.
Non-option arguments after the class name or JAR file name are passed to the main function.
The javaw command is identical to java, except that with javaw there is no associated console window. Use javaw when you don't want a command prompt window to appear. The javaw launcher will, however, display a dialog box with error information if a launch fails for some reason.
The Java 2 SDK and Java 2 Runtime Environment contain two implementations of the Java virtual machine.
The Java HotSpotTM Client VM
The Java HotSpot Client VM is the default virtual machine. Its use of Java HotSpot technology gives it superior performance to that of the Classic VM. Unless special command-line options are used to invoke the Classic VM instead (see below), java will always launch an application to run on the Client VM.
The Java 2 Classic VM
The Java 2 Classic VM is essentially the same virtual machine implementation as in version 1.2 of the Java 2 SDK. It may be invoked by using the -classic command-line option, as in this example:The Java 2 Classic VM is included only in the Java 2 SDK. It is not included in the Java 2 Runtime Environment. The -classic option will not work with the Java 2 Runtime Environment.java -classic MyApp
These are the differences between java and oldjava
This command using oldjava
- The oldjava launcher does not support the extension mechanism. Extension packages can still be used as class and resource archives.
- The oldjava launcher uses the bootstrap class loader for all classes, while the java launcher uses the bootstrap class loader only for bootstrap classes. This allows programming techniques that are incompatible with the base class loader.
- With java, the -classpath and -cp options specify a search path for user classes, and cannot be used to specify locations for bootstrap classes. With oldjava the -classpath and -cp options specify locations for all classes, including bootstrap classes.
Note that java and oldjava do not differ in their use of the CLASSPATH environment variable. Unless overridden by -classpath or -cp, CLASSPATH always specifies the location of user classes, but says nothing about the bootstrap classes.
- The oldjava launcher supports all the Java 2 platform security features. With oldjava, however, classes loaded from the class path will not have a protection domain and will therefore have all permissions which is true only for bootstrap classes with the java launcher.is equivalent to this command using javaoldjava -classpath <path> ...where in both cases <path> contains the file rt.jar. (See the SDK File Structure document for a description of rt.jar and its location in the SDK software.)java -Xbootclasspath:<path> -Djava.ext.dirs= ...
By default, the Java application launcher runs applications on the Java HotSpot Client VM, which is implemented with Java HotSpot technology. Just-in-time (JIT) compilers cannot be used in conjunction with Java HotSpot Client VM because the Java HotSpot architecture makes use of its own optimizing compiler.
Applications can optionally be launched using the Java 2 Classic VM by using the -classic option. (The Java 2 Classic VM is included only in the Java 2 SDK. It is not included in the Java 2 Runtime Environment, and the -classic option will not work with the Java 2 Runtime Environment.)In this mode, the JAVA_COMPILER environment variable can be used to specify a JIT compiler to use in conjunction with the classic VM. The .dll files for JIT compilers should be located in the directory jre\bin in the SDK, and in the bin directory in the Java 2 Runtime Environment). If the JIT compiler's file is named foo.dll, for example, setting JAVA_COMPILER equal to "foo", would cause the Java launcher to invoke the foo JIT compiler when running applications in classic mode. A JIT compiler may also be invoked when launching an application on the classic VM by setting the java.compiler property with a command-line switch:java -classic MyAppjava -classic -Djava.compiler=foo MyApp
The launcher has a set of standard options that are supported on the current runtime environment and will be supported in future releases. In addition, the default Java HotSpot Client VM provides a set of non-standard options. The non-standard options are subject to change in future releases. The Java 2 Classic VM, invoked by using the -classic option, has a different set of non-standard options which are described at Non-Standard Options of the Classic VM. The Java 2 Classic VM is included only in the Java 2 SDK. It is not included in the Java 2 Runtime Environment. The -classic option will not work with the Java 2 Runtime Environment.
- -classpath classpath
- -cp classpath
- Specify a list of directories, JAR archives, and ZIP archives to search for class files. Class path entries are separated by semicolons (;). Specifying -classpath or -cp overrides any setting of the CLASSPATH environment variable.
Used with java or javaw, -classpath or -cp only specify the class path for user classes. Used with oldjava or oldjavaw, -classpath or -cp specify the class path for both user classes and bootstrap classes.
If -classpath and -cp are not used and CLASSPATH is not set, the user class path consists of the current directory (
For more information on class paths, see Setting the Class Path.
- Set a system property value. If value is a string that contains spaces, you must enclose the string in double quotes:java -Dfoo="some string" SomeClass
- Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name. In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class: classname. Here, classname identifies the class having the public static void main(String args) method that serves as your application's starting point. See the Jar tool reference page and the Jar trail of the Java Tutorial for information about working with Jar files and Jar-file manifests.
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
The oldjava and oldjavaw tools do not support the -jar option.
- Display information about each class loaded.
- Report on each garbage collection event.
- Report information about use of native methods and other Java Native Interface activity.
- Display version information and exit.
- Display version information and continue.
- Display usage information and exit.
- Display information about non-standard options and exit.
- Operate in mixed-only mode (the default). This means that heavily used program segments (hot spots) are compiled to native code, and the remaining bytecodes are executed by a bytecode interpreter. This mode provides the fullest performance benefit offered by the Java HotSpot Client VM.
- Operate in interpreted-only mode. Compilation to native code is disabled, and all bytecodes are executed by the interpreter. The performance benefits offered by the Java HotSpot Client VM's adaptive compiler will not be present in this mode.
- Start with the debugger enabled. Refer to jdb description for more details and an example.
- Specify a semicolon-separated list of directories, JAR archives, and ZIP archives to search for boot class files. These are used in place of the boot class files included in the Java 2 SDK. Note: Applications that use this option for the purpose of overriding a class in rt.jar should not be deployed as doing so would contravene the Java 2 Runtime Environment binary code license.
- Specify a semicolon-separated path of directires, JAR archives, and ZIP archives to append to the default bootstrap class path.
- Specify a semicolon-separated path of directires, JAR archives, and ZIP archives to prepend in front of the default bootstrap class path. Note: Applications that use this option for the purpose of overriding a class in rt.jar should not be deployed as doing so would contravene the Java 2 Runtime Environment binary code license.
- Perform strict class-file format checks. For purposes of backwards compatibility, the default format checks performed by the Java 2 SDK's virtual machine are no stricter than the checks performed by 1.1.x versions of the JDK software. The -Xfuture flag turns on stricter class-file format checks that enforce closer conformance to the class-file format specification. Developers are encouraged to use this flag when developing new code because the stricter checks will become the default in future releases of the Java application launcher.
- Disable class garbage collection.
- Enable the incremental garbage collector. The incremental garbage collector, which is off by default, will eliminate occasional garbage-collection pauses during program execution. However, it can lead to a roughly 10% decrease in overall GC performance.
- Specify the initial size, in bytes, of the memory allocation pool. This value must be a multiple of 1024 greater than 1MB. Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes. The default value is 2MB. Examples:-Xms6291456 -Xms6144k -Xms6m
- Specify the maximum size, in bytes, of the memory allocation pool. This value must a multiple of 1024 greater than 2MB. Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes. The default value is 64MB. Examples:-Xmx83886080 -Xmx81920k -Xmx80m
- Profiles the running program, and sends profiling data to standard output. This option is provided as a utility that is useful in program development and is not intended to be be used in production systems.
- Enables cpu, heap, or monitor profiling. This option is typically followed by a list of comma-separated "<suboption>=<value>" pairs . Run the command java -Xrunhprof:help to obtain a list of suboptions and their default values.
- Reduces usage of operating-system signals by the Java virtual machine (JVM). This option is available beginning with J2SE 1.3.1.
In J2SE 1.3.0, the Shutdown Hooks facility was added to allow orderly shutdown of a Java application. The intent was to allow user cleanup code (such as closing database connections) to run at shutdown, even if the JVM terminates abruptly.
The JVM watches for console control events to implement shutdown hooks for abnormal JVM termination. Specifically, the JVM registers a console control handler which begins shutdown-hook processing and returns TRUE for CTRL_C_EVENT, CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, and CTRL_SHUTDOWN_EVENT.
The JVM uses a similar mechanism to implement the pre-1.2 feature of dumping thread stacks for debugging purposes. Sun's JVM uses CTRL_BREAK_EVENT to perform thread dumps.
If the JVM is run as a service (for example, the servlet engine for a web server), it can receive CTRL_LOGOFF_EVENT but should not initiate shutdown since the operating system will not actually terminate the process. To avoid possible interference such as this, the -Xrs command-line option has been added beginning with J2SE 1.3.1. When the -Xrs option is used on Sun's JVM, the JVM does not install a console control handler, implying that it does not watch for or process CTRL_C_EVENT, CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, or CTRL_SHUTDOWN_EVENT.
There are two consequences of specifying -Xrs:
- Ctrl-Break thread dumps are not available.
- User code is responsible for causing shutdown hooks to run, for example by calling System.exit() when the JVM is to be terminated.
\WARNING: Flags -Xdebug and -Xint are mutually exclusive. No more than one of those options should be used on a java command line.