Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

1.8. Java 相关命令

1.8.1. jps

		
参数	说明
-l	输出主类全名或jar路径
-q	只输出LVMID
-m	输出JVM启动时传递给main()的参数
-v	输出JVM启动时显示指定的JVM参数		
		
			

		
neo@MacBook-Pro-M2 ~> jps
6560 Jps
6273 Launcher
6274 TestThread
88034 Eclipse
6404 XMLServerLauncher
97828 org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar
1176
2891
98015 BootLanguageServerBootApp

neo@MacBook-Pro-M2 ~> jps -m
6273 Launcher /Applications/IntelliJ IDEA CE.app/Contents/plugins/java/lib/jps-builders.jar:/Applications/IntelliJ IDEA CE.app/Contents/plugins/java/lib/jps-builders-6.jar:/Applications/IntelliJ IDEA CE.app/Contents/plugins/java/lib/jps-javac-extension.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/util.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/util-8.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/util_rt.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/annotations.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/3rd-party-rt.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/protobuf.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/jps-model.jar:/Applications/IntelliJ IDEA CE.app/Contents/plugins/java/lib/javac2.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/forms_rt.jar:/Applications/IntelliJ IDEA CE.app/Contents/plugins/java/lib/aether-dependency-resolver.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar:/Applications/IntelliJ IDEA CE.ap
6274 TestThread
6562 Jps -m
88034 Eclipse
6404 XMLServerLauncher
97828 org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar -configuration /Users/neo/Library/Application Support/Code/User/globalStorage/redhat.java/1.17.0/config_mac -data /Users/neo/Library/Application Support/Code/User/workspaceStorage/c0c3760503bc4a3db6b66b0155e50c5b/redhat.java/jdt_ws
1176
2891
98015 BootLanguageServerBootApp

neo@MacBook-Pro-M2 ~> jps -l
6273 org.jetbrains.jps.cmdline.Launcher
6274 cn.netkiller.TestThread
88034 Eclipse
6564 jdk.jcmd/sun.tools.jps.Jps
6404 org.eclipse.lemminx.XMLServerLauncher
97828 /Users/neo/.vscode/extensions/redhat.java-1.17.0-darwin-arm64/server/plugins/org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar
1176
2891
98015 org.springframework.ide.vscode.boot.app.BootLanguageServerBootApp		
		
			

1.8.2. jar

查看包中的文件列表 jar -tf package.war/package.jar

		
$ /srv/java/bin/jar -tf mis.netkiller.cn-0.0.1.war |more
META-INF/
META-INF/MANIFEST.MF
WEB-INF/
WEB-INF/jsp/
WEB-INF/jsp/include/
WEB-INF/jsp/system/
WEB-INF/jsp/banner/
		
			

1.8.3. jdeps - Java class dependency analyzer.

包类依赖分析器

[net@netkiller lib]$ jdeps jersey-client-1.18.1.jar
jersey-client-1.18.1.jar -> not found
jersey-client-1.18.1.jar -> /usr/java/jdk1.8.0_73/jre/lib/rt.jar
   com.sun.jersey.api.client (jersey-client-1.18.1.jar)
      -> com.sun.jersey.api.client.async                    jersey-client-1.18.1.jar
      -> com.sun.jersey.api.client.config                   jersey-client-1.18.1.jar
      -> com.sun.jersey.api.client.filter                   jersey-client-1.18.1.jar
      -> com.sun.jersey.client.impl                         jersey-client-1.18.1.jar
      -> com.sun.jersey.client.impl.async                   jersey-client-1.18.1.jar
      -> com.sun.jersey.client.proxy                        jersey-client-1.18.1.jar
      -> com.sun.jersey.client.urlconnection                jersey-client-1.18.1.jar
      -> com.sun.jersey.core.header                         not found
      -> com.sun.jersey.core.provider                       not found
      -> com.sun.jersey.core.reflection                     not found
      -> com.sun.jersey.core.spi.component                  not found
      -> com.sun.jersey.core.spi.component.ioc              not found
      -> com.sun.jersey.core.spi.factory                    not found
      -> com.sun.jersey.core.util                           not found
      -> com.sun.jersey.spi                                 not found
      -> com.sun.jersey.spi.inject                          not found
      -> com.sun.jersey.spi.service                         not found
      -> java.io                                            
      -> java.lang                                          
      -> java.lang.annotation                               
      -> java.lang.reflect                                  
      -> java.net                                           
      -> java.util                                          
      -> java.util.concurrent                               
      -> java.util.logging                                  
      -> javax.ws.rs.core                                   not found
      -> javax.ws.rs.ext                                    not found
   com.sun.jersey.api.client.async (jersey-client-1.18.1.jar)
      -> com.sun.jersey.api.client                          jersey-client-1.18.1.jar
      -> java.lang                                          
      -> java.util.concurrent                               
   com.sun.jersey.api.client.config (jersey-client-1.18.1.jar)
      -> com.sun.jersey.core.util                           not found
      -> java.lang                                          
      -> java.util                                          
   com.sun.jersey.api.client.filter (jersey-client-1.18.1.jar)
      -> com.sun.jersey.api.client                          jersey-client-1.18.1.jar
      -> com.sun.jersey.core.util                           not found
      -> java.io                                            
      -> java.lang                                          
      -> java.net                                           
      -> java.nio.charset                                   
      -> java.security                                      
      -> java.util                                          
      -> java.util.logging                                  
      -> java.util.regex                                    
      -> java.util.zip                                      
      -> javax.ws.rs                                        not found
      -> javax.ws.rs.core                                   not found
   com.sun.jersey.client.impl (jersey-client-1.18.1.jar)
      -> com.sun.jersey.api.client                          jersey-client-1.18.1.jar
      -> com.sun.jersey.core.header                         not found
      -> java.io                                            
      -> java.lang                                          
      -> java.net                                           
      -> java.util                                          
      -> java.util.concurrent.atomic                        
      -> javax.ws.rs.core                                   not found
   com.sun.jersey.client.impl.async (jersey-client-1.18.1.jar)
      -> com.sun.jersey.api.client                          jersey-client-1.18.1.jar
      -> com.sun.jersey.api.client.async                    jersey-client-1.18.1.jar
      -> java.lang                                          
      -> java.util.concurrent                               
   com.sun.jersey.client.proxy (jersey-client-1.18.1.jar)
      -> com.sun.jersey.api.client                          jersey-client-1.18.1.jar
      -> com.sun.jersey.api.client.async                    jersey-client-1.18.1.jar
      -> java.lang                                          
      -> java.util.concurrent                               
   com.sun.jersey.client.urlconnection (jersey-client-1.18.1.jar)
      -> com.sun.jersey.api.client                          jersey-client-1.18.1.jar
      -> com.sun.jersey.core.header                         not found
      -> com.sun.jersey.spi                                 not found
      -> java.io                                            
      -> java.lang                                          
      -> java.lang.reflect                                  
      -> java.net                                           
      -> java.security                                      
      -> java.util                                          
      -> java.util.logging                                  
      -> javax.net.ssl                                      
      -> javax.ws.rs.core                                   not found
   com.sun.ws.rs.ext (jersey-client-1.18.1.jar)
      -> com.sun.jersey.core.spi.factory                    not found
      -> java.lang                                          
      -> javax.ws.rs.core                                   not found
		
		

1.8.4. JShell

JShell,即 Java Shell。从java9开始,java开始引入了 REPL(Read-Eval-Print Loop,读取-求值-输出 循环)工具

		
neo@MacBook-Pro ~ % jshell
|  Welcome to JShell -- Version 12
|  For an introduction type: /help intro

jshell> 		
		
			

1.8.4.1. /help 显示帮助信息

			
jshell> /help
|  Type a Java language expression, statement, or declaration.
|  Or type one of the following commands:
|  /list [<name or id>|-all|-start]
|  	list the source you have typed
|  /edit <name or id>
|  	edit a source entry
|  /drop <name or id>
|  	delete a source entry
|  /save [-all|-history|-start] <file>
|  	Save snippet source to a file
|  /open <file>
|  	open a file as source input
|  /vars [<name or id>|-all|-start]
|  	list the declared variables and their values
|  /methods [<name or id>|-all|-start]
|  	list the declared methods and their signatures
|  /types [<name or id>|-all|-start]
|  	list the type declarations
|  /imports 
|  	list the imported items
|  /exit [<integer-expression-snippet>]
|  	exit the jshell tool
|  /env [-class-path <path>] [-module-path <path>] [-add-modules <modules>] ...
|  	view or change the evaluation context
|  /reset [-class-path <path>] [-module-path <path>] [-add-modules <modules>]...
|  	reset the jshell tool
|  /reload [-restore] [-quiet] [-class-path <path>] [-module-path <path>]...
|  	reset and replay relevant history -- current or previous (-restore)
|  /history [-all]
|  	history of what you have typed
|  /help [<command>|<subject>]
|  	get information about using the jshell tool
|  /set editor|start|feedback|mode|prompt|truncation|format ...
|  	set configuration information
|  /? [<command>|<subject>]
|  	get information about using the jshell tool
|  /! 
|  	rerun last snippet -- see /help rerun
|  /<id> 
|  	rerun snippets by ID or ID range -- see /help rerun
|  /-<n> 
|  	rerun n-th previous snippet -- see /help rerun
|  
|  For more information type '/help' followed by the name of a
|  command or a subject.
|  For example '/help /list' or '/help intro'.
|  
|  Subjects:
|  
|  intro
|  	an introduction to the jshell tool
|  keys
|  	a description of readline-like input editing
|  id
|  	a description of snippet IDs and how use them
|  shortcuts
|  	a description of keystrokes for snippet and command completion,
|  	information access, and automatic code generation
|  context
|  	a description of the evaluation context options for /env /reload and /reset
|  rerun
|  	a description of ways to re-evaluate previously entered snippets
			
			
				

介绍信息

			
jshell> /help intro
|  
|                                   intro
|                                   =====
|  
|  The jshell tool allows you to execute Java code, getting immediate results.
|  You can enter a Java definition (variable, method, class, etc), like:  int x = 8
|  or a Java expression, like:  x + x
|  or a Java statement or import.
|  These little chunks of Java code are called 'snippets'.
|  
|  There are also the jshell tool commands that allow you to understand and
|  control what you are doing, like:  /list
|  
|  For a list of commands: /help			
			
				

1.8.4.2. 退出命令

			
jshell> /exit
|  Goodbye
			
				

1.8.5. jlink

使用 jlink 定自己的 jre

		
jlink --module-path jmods --add-modules java.desktop --output /tmp/jre-16.0.2	
		
			

查看输出结果

		
neo@MacBook-Pro-Neo ~ % ll /tmp/jre-16.0.2
total 8
drwxr-xr-x   4 neo  wheel   128B  8 31 15:08 bin
drwxr-xr-x   5 neo  wheel   160B  8 31 15:08 conf
drwxr-xr-x   8 neo  wheel   256B  8 31 15:08 include
drwxr-xr-x   7 neo  wheel   224B  8 31 15:08 legal
drwxr-xr-x  38 neo  wheel   1.2K  8 31 15:08 lib
drwxr-xr-x   3 neo  wheel    96B  8 31 15:08 man
-rw-r--r--   1 neo  wheel    93B  8 31 15:08 release		
		
			

查看 release 文件

		
neo@MacBook-Pro-Neo ~ % cat /tmp/jre-16.0.2/release 
JAVA_VERSION="16.0.2"
MODULES="java.base java.datatransfer java.xml java.prefs java.desktop"