首页 > 编程语言 >Java Tool - java 命令

Java Tool - java 命令

时间:2024-06-09 09:11:26浏览次数:25  
标签:set Java option default Tool XX java size

参考资料

java命令用于启动 1 个 Java 应用,即启动一个 JVM 进程。

1 语法

java [_options_] _classname_ [_args_]

java [_options_] -jar _filename_ [_args_]
  • options - 空格分隔的命令行选项详见 Options 部分.
  • classname - 启动的类名(完整类型,例如: com.black.app.Bootstrap)
  • filename - 启动 jar 包名,仅和-jar 共同使用(jar 包中必须声名 Main-Class manifest header )。
  • args - 启动类 main 方法参数,空格分隔

2 启动 Java 应用过程

java命令启动 java 应用的过程:

  • 启动 Java 运行环境(JRE)
  • 加载声名的 class
  • 调用声名的 class 的 main 方法,main 方法如下:
public static void main(String[] args)

java命令也可以通过以下方式启动 JavaFx 应用:

  • 要么加载一个启动类的 main 方法
  • 要么加载继承javafx.application.Application的类:
    • 构建javafx.application.Application类的实例
    • 调用该实例的 init() 方法
    • 调用 start(javafx.stage.Stage)  方法

3 选项

The java command supports a wide range of options that can be divided into the following categories:

  • Standard Options - JVM 实现必须支持的选项。
  • Non-Standard Options - HotSpot VM 专有,以 -X开头。
  • Advanced Runtime Options - 开发者选项,用于调整 HotSpot VM,以 -XX开头
  • Advanced JIT Compiler Options - HotSpot VM 即时编译器选项
  • Advanced Serviceability Options - 收集系统信息和执行调试
  • Advanced Garbage Collection Options - 垃圾回收器选项

3.1 生产常用选项

java -cp /jdk/lib/*.jar;/jdk/jre/lib/* -Xms512m -Xmx1024m  -Dspring.profiles.active=dev  -XX:ErrorFile=/var/log/java/java_error.log -XX:+HeapDumpOnOutOfMemoryError  -XX:HeapDumpPath=/var/log/java/java_heapdump.hprof -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+PrintCommandLineFlags -XX:+PrintTenuringDistribution -jar hello.jar
分类 选项 说明
Standard Options -cp <目录和 zip/jar 文件的类搜索路径>
-classpath <目录和 zip/jar 文件的类搜索路径>
指定加载的 zip或jar类搜索路径,用于查找加载的类
Standard Options -D_property_=value 代码里可通过 System.getProperties()System.getProperty(String key)
用例:-Dfoo="foo bar"
Standard Options -jar filename 启动 jar 包,jar包内需包含声名 Main-Class manifest header
Non-Standard Options -Xms jvm 堆最小内存
Non-Standard Options -Xmx jvm 堆最大内存
Non-Standard Options -Xmn jvm 堆年轻代大小
Advanced Runtime Options -XX:ErrorFile=filename 如果不指定会使用 TMP 环境变量目录,示例:-XX:ErrorFile=./hs_err_pid%p.log
Advanced Serviceability Options -XX:+HeapDumpOnOutOfMemoryError 开启 oom dump
Advanced Serviceability Options -XX:HeapDumpPath=path -XX:HeapDumpPath=/var/log/java/java_heapdump.hprof
Advanced Garbage Collection Options -XX:+PrintGC
-XX:+PrintGCDetails
打印每次 GC 详细信息
Advanced Runtime Options -XX:+PrintCommandLineFlags 打印命令行参数
Advanced Garbage Collection Options -XX:+PrintGCDateStamps
-XX:+PrintGCTimeStamps
打印每次 GC的日期和时间
Advanced Garbage Collection Options -XX:+PrintTenuringDistribution 打印 tenuring age 信息

3.2 Standard Options

选项&默认值 说明
-agentlib:libname[=options] Loads the specified native agent library. After the library name, a comma-separated list of options specific to the library can be used.

If the option -agentlib:foo is specified, then the JVM attempts to load the library named libfoo.so in the location specified by the LD_LIBRARY_PATH system variable (on OS X this variable is DYLD_LIBRARY_PATH).

The following example shows how to load the heap profiling tool (HPROF) library and get sample CPU information every 20 ms, with a stack depth of 3:

-agentlib:hprof=cpu=samples,interval=20,depth=3

The following example shows how to load the Java Debug Wire Protocol (JDWP) library and listen for the socket connection on port 8000, suspending the JVM before the main class loads:

-agentlib:jdwp=transport=dt_socket,server=y,address=8000

For more information about the native agent libraries, refer to the following:

- The java.lang.instrument package description at [http://docs.oracle.com/javase/8/docs/api/java/lang/instrument/package-summary.html](http://docs.oracle.com/javase/8/docs/api/java/lang/instrument/package-summary.html)

- Agent Command Line Options in the JVM Tools Interface guide at [http://docs.oracle.com/javase/8/docs/platform/jvmti/jvmti.html#starting](http://docs.oracle.com/javase/8/docs/platform/jvmti/jvmti.html#starting)
-agentpath:pathname[=options] Loads the native agent library specified by the absolute path name. This option is equivalent to -agentlib but uses the full path and file name of the library.
-client Selects the Java HotSpot Client VM. The 64-bit version of the Java SE Development Kit (JDK) currently ignores this option and instead uses the Server JVM.

For default JVM selection, see Server-Class Machine Detection at
[http://docs.oracle.com/javase/8/docs/technotes/guides/vm/server-class.html](http://docs.oracle.com/javase/8/docs/technotes/guides/vm/server-class.html)

-D_property_=value
Sets a system property value. The property variable is a string with no spaces that represents the name of the property. The value variable is a string that represents the value of the property. If value is a string with spaces, then enclose it in quotation marks (for example -Dfoo="foo bar").
-d32 Runs the application in a 32-bit environment. If a 32-bit environment is not installed or is not supported, then an error will be reported. By default, the application is run in a 32-bit environment unless a 64-bit system is used.
-d64 Runs the application in a 64-bit environment. If a 64-bit environment is not installed or is not supported, then an error will be reported. By default, the application is run in a 32-bit environment unless a 64-bit system is used.

Currently only the Java HotSpot Server VM supports 64-bit operation, and the -server option is implicit with the use of -d64. The -client option is ignored with the use of -d64. This is subject to change in a future release.
-disableassertions[:[packagename]...|:classname]或
-da[:[packagename]...|:classname]
Disables assertions. By default, assertions are disabled in all packages and classes.

With no arguments, -disableassertions (-da) disables assertions in all packages and classes. With the packagename argument ending in ..., the switch disables assertions in the specified package and any subpackages. If the argument is simply ..., then the switch disables assertions in the unnamed package in the current working directory. With the classname argument, the switch disables assertions in the specified class.

The -disableassertions (-da) option applies to all class loaders and to system classes (which do not have a class loader). There is one exception to this rule: if the option is provided with no arguments, then it does not apply to system classes. This makes it easy to disable assertions in all classes except for system classes. The -disablesystemassertions option enables you to disable assertions in all system classes.

To explicitly enable assertions in specific packages or classes, use the -enableassertions (-ea) option. Both options can be used at the same time. For example, to run the MyClass application with assertions enabled in package com.wombat.fruitbat (and any subpackages) but disabled in class com.wombat.fruitbat.Brickbat, use the following command:

java -ea:com.wombat.fruitbat... -da:com.wombat.fruitbat.Brickbat MyClass
-disablesystemassertions

-dsa
Disables assertions in all system classes.
-enableassertions[:[packagename]...|:classname]

-ea[:[packagename]...|:classname]
Enables assertions. By default, assertions are disabled in all packages and classes.

With no arguments, -enableassertions (-ea) enables assertions in all packages and classes. With the packagename argument ending in ..., the switch enables assertions in the specified package and any subpackages. If the argument is simply ..., then the switch enables assertions in the unnamed package in the current working directory. With the classname argument, the switch enables assertions in the specified class.

The -enableassertions (-ea) option applies to all class loaders and to system classes (which do not have a class loader). There is one exception to this rule: if the option is provided with no arguments, then it does not apply to system classes. This makes it easy to enable assertions in all classes except for system classes. The -enablesystemassertions option provides a separate switch to enable assertions in all system classes.

To explicitly disable assertions in specific packages or classes, use the -disableassertions (-da) option. If a single command contains multiple instances of these switches, then they are processed in order before loading any classes. For example, to run the MyClass application with assertions enabled only in package com.wombat.fruitbat (and any subpackages) but disabled in class com.wombat.fruitbat.Brickbat, use the following command:

java -ea:com.wombat.fruitbat... -da:com.wombat.fruitbat.Brickbat MyClass
-enablesystemassertions

-esa

Enables assertions in all system classes.
-help

-?
Displays usage information for the java command without actually running the JVM.
-jar filename Executes a program encapsulated in a JAR file. The filename argument is the name of a JAR file with a manifest that contains a line in the form Main-Class:``classname that defines the class with the public static void main(String[] args) method that serves as your application's starting point.

When you use the -jar option, the specified JAR file is the source of all user classes, and other class path settings are ignored.

For more information about JAR files, see the following resources:

- jar(1)

- The Java Archive (JAR) Files guide at [http://docs.oracle.com/javase/8/docs/technotes/guides/jar/index.html](http://docs.oracle.com/javase/8/docs/technotes/guides/jar/index.html)

- Lesson: Packaging Programs in JAR Files at
[http://docs.oracle.com/javase/tutorial/deployment/jar/index.html](http://docs.oracle.com/javase/tutorial/deployment/jar/index.html)

-javaagent:jarpath[=options] Loads the specified Java programming language agent. For more information about instrumenting Java applications, see the java.lang.instrument package description in the Java API documentation at [http://docs.oracle.com/javase/8/docs/api/java/lang/instrument/package-summary.html](http://docs.oracle.com/javase/8/docs/api/java/lang/instrument/package-summary.html)

-jre-restrict-search

Includes user-private JREs in the version search.
-no-jre-restrict-search Excludes user-private JREs from the version search.
-server Selects the Java HotSpot Server VM. The 64-bit version of the JDK supports only the Server VM, so in that case the option is implicit.

For default JVM selection, see Server-Class Machine Detection at
[http://docs.oracle.com/javase/8/docs/technotes/guides/vm/server-class.html](http://docs.oracle.com/javase/8/docs/technotes/guides/vm/server-class.html)
-showversion Displays version information and continues execution of the application. This option is equivalent to the -version option except that the latter instructs the JVM to exit after displaying version information.
-splash:imgname Shows the splash screen with the image specified by imgname. For example, to show the splash.gif file from the images directory when starting your application, use the following option:
<br>-splash:images/splash.gif<br>
-verbose:class Displays information about each loaded class.
-verbose:gc Displays information about each garbage collection (GC) event.
-verbose:jni Displays information about the use of native methods and other Java Native Interface (JNI) activity.
-version Displays version information and then exits. This option is equivalent to the -showversion option except that the latter does not instruct the JVM to exit after displaying version information.
-version:release Specifies the release version to be used for running the application. If the version of the java command called does not meet this specification and an appropriate implementation is found on the system, then the appropriate implementation will be used.

The release argument specifies either the exact version string, or a list of version strings and ranges separated by spaces. A version string is the developer designation of the version number in the following form: 1.x.0_u (where x is the major version number, and u is the update version number). A version range is made up of a version string followed by a plus sign (+) to designate this version or later, or a part of a version string followed by an asterisk (*) to designate any version string with a matching prefix. Version strings and ranges can be combined using a space for a logical OR combination, or an ampersand (&) for a logical AND combination of two version strings/ranges. For example, if running the class or JAR file requires either JRE 6u13 (1.6.0_13), or any JRE 6 starting from 6u10 (1.6.0_10), specify the following:

-version:"1.6.0_13 1.6* & 1.6.0_10+"

Quotation marks are necessary only if there are spaces in the release parameter.

For JAR files, the preference is to specify version requirements in the JAR file manifest rather than on the command line.

3.3 Non-Standard Options

选项 说明
-X Displays help for all available -X options.
-Xbatch Disables background compilation. By default, the JVM compiles the method as a background task, running the method in interpreter mode until the background compilation is finished. The -Xbatch flag disables background compilation so that compilation of all methods proceeds as a foreground task until completed.

This option is equivalent to -XX:-BackgroundCompilation.
-Xbootclasspath:path Specifies a list of directories, JAR files, and ZIP archives separated by colons (

标签:set,Java,option,default,Tool,XX,java,size
From: https://www.cnblogs.com/lihw-study/p/18239235

相关文章

  • 从零手写实现 nginx-01-为什么不能有 java 版本的 nginx?
    前言大家好,我是老马。很高兴遇到你。作为一个java开发者,工作中一直在使用nginx。却发现一直停留在使用层面,无法深入理解。有一天我在想,为什么不能有一个java版本的nginx呢?一者是理解nginx的设计灵魂,再者java开发者用java语言的服务器不是更加自然吗。于是......
  • JavaScript:从基础到进阶的全面介绍
    JavaScript:从基础到进阶的全面介绍JavaScript(简称JS)是一种广泛用于Web开发的编程语言。它是一种轻量级的、解释型或即时编译的语言,具有函数优先的特点。JS最初是为了实现网页的动态效果而设计的,如今已发展成为前端开发、服务器端开发、移动开发等多个领域的重要工具。本文......
  • JavaWeb中,web应用的上下文路径解读
    当前Web应用的上下文路径(ContextPath)指的是Web应用在服务器上的根路径。在Servlet或JSP环境中,一个服务器可以运行多个Web应用,每个应用都有一个唯一的上下文路径。例如,如果你的Web应用部署在Tomcat服务器上,并且在Tomcat的webapps目录下有一个名为myapp的Web应用,那么这个应......
  • Java第二次Blog
    7-4~6题目集前言这些题目主要用到对象与类的处理继承与多态的使用:继承和多态是面向对象编程中相互关联的两个概念。继承为多态提供了基础,而多态则通过继承实现了代码的灵活性和可扩展性。1.字符串处理:需要对输入的题目信息和答题信息进行字符串分割、提取和处理,以获取题目编号......
  • 重生之我在男航学Java-2
    一、前言答题判题程序-4新增内容:选择题,填空题,输出顺序的变化,考虑多个同学有多张不同试卷的答卷的情况新增知识点:抽象类的运用;在设计好前三次的题目集的情况下,将原本的题目类设计为抽象类(本来在第一次题目集的时候就可以如此设计,但是当时学的不够多),同时将输入的信息进行判断是......
  • Javascript全解(基础篇)
     语法与数据类型语法var\let\constvar声明一个变量,可选初始化一个值。let声明一个块作用域的局部变量,可选初始化一个值。const声明一个块作用域的只读常量。用 var 或 let 语句声明的变量,如果没有赋初始值,则其值为 undefined。如果访问一个未声明的变量会导致......
  • JavaScript html css 字符串对象
    字符串对象字符串所有的方法,都不会修改字符串本身(字符串是不可变的),操作完成会返回一个新的字符串。length属性作用:获取字符串长度示例:<spanstyle="background-color:#f8f8f8"><spanstyle="color:#333333"><spanstyle="color:#770088">let</span><spanstyle=......
  • 力扣96 不同的二叉搜索树 Java版本
    文章目录题目描述代码题目描述给你一个整数n,求恰由n个节点组成且节点值从1到n互不相同的二叉搜索树有多少种?返回满足题意的二叉搜索树的种数。示例1:输入:n=3输出:5示例2:输入:n=1输出:1提示:1<=n<=19代码importjava.lang.annotation.Rete......
  • Java循环结构
    Java循环结构循环结构可以看成是条件判断语句和转向语句的结合。循环结构是指在程序中需要反复执行某个功能而设置的一种程序结构。它由循环体中的条件,判断继续执行某个功能还是退出循环。根据判断条件,循环结构又可细分为以下两种形式:先判断后执行的循环结构和先执行后判断......
  • tools maven引入 maven tools.jar
    怎么用javadoc和Doclet配合解析自己想要的注释(链接)。既然是一个工具,自然就要生成可执行的jar包。这貌似是一个很合理的要求,然后坑就来了。我上篇说的是直接复制的tool.jar到lib包下面,添加进资源包就可以了,但是maven项目肯定不能这样做的,这样不规范。我在网上去搜索了tools.jar的......