Java Command Parameters

Source: Internet
Author: User

Http://hi.baidu.com/anlylove/blog/item/b4a0cc1fa9779cffe0fe0bd3.html

When Java runs compiled classes, it is loaded and executed through the Java Virtual Machine. the Java Virtual Machine is started by running the operating system command java_home \ bin \ Java-option. The-option parameter is the virtual machine parameter, java_home is the JDK installation path. With these parameters, you can adjust the running status of the virtual machine. You can have a deeper understanding of the running mode of the virtual machine.

1. view the parameter list:

Virtual Machine Parameters are classified into two types: basic and extension. Enter java_home \ bin \ Java in the command line to get the basic parameter list,
Enter java_home \ bin \ Java-X in the command line to obtain the extended parameter list.

Ii. Basic parameter description:

1.-client,-Server

These two parameters are used to set the running mode used by the virtual machine. The client mode is faster to start, but the runtime performance and memory management efficiency are not as good as the server mode, which is usually used for client applications. On the contrary, server mode is slower to start than client, but can achieve higher running performance.

In Windows, the default Virtual Machine Type is client mode. To use server mode, you need to add the-server parameter when starting the virtual machine to achieve higher performance and server-side applications, server mode is recommended, especially for multiple CPU Systems. In Linux, Solaris adopts the server mode by default.

2.-hotspot

The meaning is the same as that of the client. The parameter jdk1.4 previously used is no longer used and replaced by the client.

3.-classpath,-CP

When a virtual machine runs a class, it needs to be loaded into the memory. The method and sequence of the Virtual Machine search class are as follows:

Bootstrap classes, extension classes, and user classes.

The path in Bootstrap is the jar or ZIP file that comes with the VM. The VM first searches for these package files and uses the system. getproperty ("Sun. boot. class. path ") to obtain the package name searched by the virtual machine.

Extension is a jar file located in the JRE "lib" ext directory. The VM searches for the JAR file in this directory after searching for bootstrap. Use System. getproperty ("Java. Ext. dirs") to obtain the extension search path used by the VM.

The user classes search order is the current directory, environment variable classpath, and-classpath.

4.-classpath

Tell the virtual machine to search for the directory name, JAR file name, and zip file name, separated by semicolons.

For example, if you develop a public class and package it into a common class. jar package, in the use of common. -classpath common is required for classes in jar. jar tells the virtual machine from common. find the class in jar, otherwise the virtual machine will throw Java. lang. noclassdeffounderror: the class definition is not found.

You can use system. getproperty ("Java. Class. Path") at runtime to obtain the path of the Virtual Machine search class.

After-classpath is used, the virtual machine no longer uses the Class search path in classpath. If-classpath and classpath are not set, the virtual machine uses the current path (.) as the class search path.

We recommend that you use-classpath to define the classpath to be searched by the virtual machine, instead of using the search path of the Environment Variable classpath, to reduce the potential conflicts when multiple projects use classpath at the same time. For example, application 1 must use Class G in a1.0.jar, Application 2 must use Class G in a2.0.jar, and a2.0.jar is the upgrade package of a1.0.jar. When a1.0.jar and a2.0.jar are both in classpath, when the Virtual Machine finds the class G in the first package, the search is stopped. If the Virtual Machine of Application 1 Application 2 searches from the classpath, a class G with the correct version of the application will not be available.

5.-D <propertyname> = Value

Set the attribute name/value pair in the system attribute of the Virtual Machine. applications running on the virtual machine can obtain the value of system. getproperty ("propertyname.

If the value contains spaces, double quotation marks are required to enclose the value, such as-dname = "Space string ".

This parameter is usually used to set system-level global variable values. For example, the configuration file path should be accessible from anywhere in the program.
This article comes from zhimeng

6.-verbose [: Class | GC | JNI]

Displays the virtual machine running information on the output device.

Verbose and verbose: the meaning of the class is the same. The output information of the class loaded by the virtual machine is as follows:

[Loaded java. Io. filepermission $1 from shared objects file]

This parameter can be used to diagnose the situation of loading a virtual machine from a class if the reporting class of the virtual machine cannot be found or the class conflict occurs.

7.-verbose: GC

When the VM memory is recycled, the output device is displayed in the following format:

[Full GC 268 K-> 168 K (1984 K), 0.0187390 secs]

This parameter is used to monitor the recovery of VM memory.

8.-verbose: JNI

When the virtual machine calls the native method, the output device information is displayed in the following format:

[Dynamic-linking native method hellonative. Sum... JNI]

This parameter is used to monitor Virtual Machine calls to local methods. It can be conveniently diagnosed when a JNI error occurs.

9.-version

Display the version of the virtual machine that can be run and exit. When a machine is installed with JDK of different versions

10.-showversion

Displays version information and help information. Content from dedecms

11.-ea [: <packagename>... |: <classname>]

12.-enableassertions [: <packagename>... |: <classname>]

JAVA supports the assertion mechanism from jdk1.4 to diagnose runtime problems. Usually, assertions are valid in the test phase and do not need to be run during formal operation. The expression value after assertion is a logical value. If it is true, the assertion does not run. If it is false, the assertionerror java. Lang. assertionerror is thrown.

The above parameters are used to set whether the VM starts the assertion mechanism. If there is a shortage of time-saving VMS, the assertion mechanism is disabled and the-EA can be used to enable the assertion mechanism, when <packagename> and classname are not added, assertions in all packages and classes are run. If you want to run only assertions in some packages or classes, you can add the package name or class name to-ea. For example, to start the assertions in the com. Foo. util package, run the command-Ea: COM. Foo. util.

13.-Da [: <packagename>... |: <classname>]

14.-disableassertions [: <packagename>... |: <classname>]

Set the VM to disable assertion processing. packagename and classname are used in the same way as-ea.

15.-ESA |-enablesystemassertions

Sets the virtual machine to display system-class assertions.

16.-DSA |-disablesystemassertions

Set the VM to disable system-class assertions. Dedecms.com

17.-agentlib: <libname> [= <Options>]

This parameter is newly introduced by jdk5. it is used to load the local proxy library on a virtual machine.

Libname is the name of the local agent library, and the virtual machine search path is the path in the environment variable path. options is the parameter passed to the local database for startup. Multiple parameters are separated by commas. Search for the local database name libname on Windows. DLL file. The local library name is libname searched on the Unix virtual machine. so files, search PATH environment variables vary on different systems, Linux, SunOS, IRIX for LD_LIBRARY_PATH, Aix for libpath, HP-UX for shlib_path.

For example, you can use-agentlib: hprof to obtain the running status of a virtual machine, including running data such as CPU, memory, and thread, and output data to a specified file.-agentlib is available: hprof = help to get the help list. The hprof. dll file can be found in the JRE "bin directory.

18.-agentpath: <pathname> [= <Options>]

Set the VM to load the local database in full path without searching the path in path. Other functions are the same as those of agentlib.

19.-javaagent: <jarpath> [= <Options>]

When the virtual machine is started, the Java language device proxy is entered. The mainfest file in the jarpath file must have the agent-class attribute. The proxy class must implement the public static void premain (string agentargs, instrumentation insT) method. When the VM is initialized, The premain method is called in the order described by the proxy class. Copyright dedecms

See java. Lang. Instrument

Iii. Description of extended parameters

1.-xmixed

In setup-client mode, the VM compiles and executes just-in-time for frequently used methods, and interprets other methods for execution. This is the default virtual machine mode.

2.-xint

Set-the virtual machine running in client mode executes the class bytecode in interpreted mode, and does not compile the bytecode as the local code.

3.-xbootclasspath: Path

4.-xbootclasspath/A: Path

5.-xbootclasspath/P: Path

Change the virtual machine to load the default system running package Rt. jar and load the system running class from the search path set in-xbootclasspath. This parameter is not used unless you can write a runtime.

/A: add the search path in path to the default search path.

/P: Search for the search path in path before the default search path.

6.-xnoclassgc

Disable the class garbage collection function of the virtual machine.

7.-xincgc

Start the incremental garbage collector, Which is disabled by default. The incremental garbage collector can reduce the pause time caused by accidental long-time garbage collection. However, the incremental garbage collector and the application execute concurrently, so it occupies part of the CPU's functions on the application.

Zhimeng Content Management System

8.-xloggc: <File>

Write the garbage collection information of the virtual machine to the log file. The file name is specified by the file. The file format is a flat file, and the content is the same as that output by-verbose: GC.

9.-xbatch

The default running mode of the VM is to compile the class code in the background, and then execute the code in the foreground. Using the-xbatch parameter, the VM background compilation will be disabled and then executed after the foreground compilation is complete.

10.-XMS <size>

Set the initial size of the available memory heap of the VM. The default unit is byte. The size is an integer multiple of 1024 and must be greater than 1 MB. The available size is K or M (m) to set a large memory size. The initial heap size is 2 MB.

Example:-xms6400k,-xms256m

11.-xmx <size>

Sets the maximum available size of the VM memory heap. The default unit is byte. The value must be an integer multiple of 1024 and greater than 2 MB. You can set a large memory size in the unit of K (k) or M (m. The maximum heap size is 64 MB by default.

Example:-xmx81920k,-xmx80m

If the Virtual Machine throws a java. Lang. outofmemoryerror: Java heap space error when the application has applied for a large memory runtime, you need to use-xmx to set the media to be selected? BR>

12.-XSS <size>

Sets the size of the thread stack. The default unit is byte. Similar to-xmx, you can use K or m to set a large value. Generally, the default size allocated by the operating system to the thread stack is 1 MB.
This article comes from zhimeng

You can also set the stack size when creating a thread object in Java. The prototype of the constructor is thread (threadgroup group, runnable target, string name, long stacksize ).

13.-xprof

Outputs the diagnostic information when the CPU is running.

14.-xfuture

Perform a strict format check on the class files to ensure that the class Code complies with the class code specifications. To maintain backward compatibility, virtual machines do not perform strict format checks by default.

15.-xrs

Reduce the use of Operating System signals (singals) in virtual machines. This parameter is usually used when a virtual machine is running in a later service mode (such as servlet ).

16.-xcheck: JNI
1. Run the class file
Execute the class file with the main method. Command Behavior:
Java <class file name>

Note: do not include the file suffix. Class in the class file name.

For example:
Java Test

If the executed class file contains a package, it is used in the class file:
Package <package name>

The command should be executed in the package's base path. The command behavior is as follows:
Java <package name>. Class file name

For example:
In packagetest. Java, the package name is com. ee2ee. test. The corresponding statement is:
Package com. ee2ee. test;

Packagetest. Java and the compiled class file packagetest. Class are stored in the following directory:

Classes
|__ Com
|__ Ee2ee
|__ Test
|__ Packagetest. Java
|__ Packagetest. Class

To run packagetest. Class, run the following command in the classes directory:
Java com. ee2ee. Test. packagetest

Ii. Run the class in the jar file
The principle is the same as running the class file. You only need to add the parameter-CP <JAR File Name>.
For example, run com. ee2ee. Test. packagetest in test. Jar. The command line is as follows:
Java-CP test. Jar com. ee2ee. Test. packagetest

3. Display JDK version information
When a machine has multiple JDK versions, you need to know which version of JDK is currently in use. You can use the-version parameter to know the version. Command Behavior:
Java-version

4. Increase the maximum memory available for virtual machines
The maximum memory available for Java virtual machines is limited. The default value is usually 64 MB or 128 MB.

If an application loads data into memory to improve performance and occupies a large amount of memory, such as exceeding the default maximum of 128 MB, it needs to increase the maximum memory available for the Java Virtual Machine, otherwise, an out of Memory exception occurs. When starting Java, you need to use the following two parameters:

-Memory size used for XMS Java VM Initialization
-Maximum memory available for xmx Java Virtual Machine

The size set in the preceding two parameters can contain units. For example, 256 MB indicates MB.

Example:
Java-xms128m-xmx256m...
Indicates that the memory used for Java VM Initialization is 128 MB, and the maximum memory available is 256 MB.

For tomcat, you can modify its script Catalina. Sh (UNIX platform) or Catalina. BAT (Windows platform) and set the variable java_opts. For example:
Java_opts = '-xms128m-xmx256m'

In the console output information, there is a-X (note that it is in upper case) command, which is the command for viewing JVM configuration parameters.
Next, run the Java-x command to view the JVM configuration instructions:
The following result is the secret weapon for configuring JVM parameters. The information is in English and can be easily read, I have translated it into Chinese Based on my own understanding (if it is not accurate, ask Boyou to make an axe)
-----------------------------------------------------------------------
D: \ j2sdk15 \ bin> JAVA-x
-Xmixed mixed mode execution (default)
-Xint interpreted mode execution only
-Xbootclasspath: <directories and zip/jar files separated by;>
Set search path for bootstrap classes and resources
-Xbootclasspath/A: <directories and zip/jar files separated by;>
Append to end of Bootstrap class path
-Xbootclasspath/P: <directories and zip/jar files separated by;>
Prepend in front of Bootstrap class path
-Xnoclassgc disable class garbage collection
-Xincgc enable incremental garbage collection
-Xloggc: <File> log GC status to a file with time stamps
-Xbatch disable background Compilation
-XMS <size> set initial Java heap size
-Xmx <size> set maximum Java heap size
-XSS <size> set Java thread stack size
-Xprof output CPU profiling data
-Xfuture enable strictest checks, anticipating future default
-Xrs reduce use of OS signals by Java/Vm (see Documentation)
-Xcheck: JNI perform. Additional checks for JNI Functions
-Xshare: off do not attempt to use shared class data
-Xshare: auto use shared class data if possible (default)
-Xshare: On require using shared class data, otherwise fail.
The-x options are non-standard and subject to change without notice.
-----------------------------------------------------------------------
JVM configuration parameters:
-----------------------------------------------------------------------
1.-xmixed mixed mode execution (default)
Mixed Mode execution
2.-xint interpreted mode execution only
Explain mode execution
3.-xbootclasspath: <directories and zip/jar files separated by;>
Set search path for bootstrap classes and resources
Set the directory path for storing zip/jar resources or classes (. class files)
3.-xbootclasspath/A: <directories and zip/jar files separated by;>
Append to end of Bootstrap class path
Append zip/jar resources or classes (. class files) to the directory
4.-xbootclasspath/P: <directories and zip/jar files separated by;>
Prepend in front of Bootstrap class path
Pre-load zip/jar resources or class (. Class file) storage directory path
5.-xnoclassgc disable class garbage collection
Disable garbage collection
6.-xincgc enable incremental garbage collection
Enable the garbage collection function
7.-xloggc: <File> log GC status to a file with time stamps
Record the garbage return log to a file.
8.-xbatch disable background Compilation
Disable background Compilation
9.-XMS <size> set initial Java heap size
Set the JVM initialization heap memory size
10.-xmx <size> set maximum Java heap size
Sets the maximum JVM heap memory size.
11.-XSS <size> set Java thread stack size
Set JVM stack memory size
12.-xprof output CPU profiling data
Input CPU summary table data
13.-xfuture enable strictest checks, anticipating future default
Execute strict code check to predict possible situations
14.-xrs reduce use of OS signals by Java/Vm (see Documentation)
Restore Operating System signals through JVM
15.-xcheck: JNI perform. Additional checks for JNI Functions
Check JNI Functions
16.-xshare: off do not attempt to use shared class data
Try not to use shared data
17.-xshare: auto use shared class data if possible (default)
Use shared data whenever possible
18.-xshare: On require using shared class data, otherwise fail.
Use shared data whenever possible, otherwise the operation fails
The-x options are non-standard and subject to change without notice.
-----------------------------------------------------------------------
How can we use these parameters? In fact, all command lines are used in this way. The following is a simple example of helloworl to demonstrate the usage of this parameter, which is very simple.
Helloworld. Java
-----------------------------------------------
Public class helloworld
{
Public static void main (string [] ARGs)
{
System. Out. println ("Hello world! ");
}
}
Compile and run:
D: \ j2sdk15 \ bin> javac helloworld. Java
D: \ j2sdk15 \ bin> JAVA-xms256m-xmx512m helloworld
Hello world!
Oh, that's enough!
Practice: Configure JVM parameters in large systems or applications
For example, if you configure the parameters of the IDE tool, idea and eclipse are common, you can specify them in a configuration file.
If you want to configure these parameters in the J2EE environment, you need to specify them in the startup parameter settings of the J2EE application server or servlet container, and configure them in the startup file. Tomcat is in Catalina. in bat, Weblogic and WebSphere are configured in other places. I will talk about them. I believe anyone who has played these large servers knows that I have never read this article before, you will know when you are playing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.