【Java工具】Jps命令
阿新 • • 發佈:2018-12-09
一、命令說明
jps(Java Virtual Machine Process Status Tool)是JDK1.5提供的一個顯示當前Java程序的工具(類似Linux作業系統上的ps命令),這個命令主要用於顯示當前有哪些Java程序,可以說後續jstat,jstack等命令的基礎。
二、引數說明
C:\Users\Administrator>jps -help
usage: jps [-help]
jps [-q] [-mlvV] [<hostid>]
Definitions:
<hostid>: <hostname >[:<port>]
1 . jps -q: 顯示當前執行的Java程序ID
C:\Users\Administrator>jps -q
4816
7952
5988
6456
2 . jps -m : 顯示當前執行的Java程序ID + Main方法args引數
C:\Users\Administrator>jps -m
7952
5988
6456 Bootstrap start
9276 Jps -m
3 .jps -l : 顯示當前執行的Java程序ID + Main方法所在類的完整名稱
C:\Users\Administrator>jps -l
7952
5988
7252 sun.tools.jps.Jps
6456 org.apache.catalina.startup.Bootstrap
4 . jps -v : 顯示當前執行的Java程序ID + JVM引數
7952 -Dosgi.requiredJavaVersion=1.8 -Xms40m -Dosgi.module.lock.timeout=10 -Dorg.eclipse.swt.browser.IEVersion=10001 -Xmx1200m
5988 -Dosgi.requiredJavaVersion=1.8 -XX:+UseG1GC -XX:+UseStringDeduplication -Dosgi.requiredJavaVersion =1.8 -Xms512m -Xmx1280m -Dfile.encoding=UTF-8 -DtolerateI
llegalAmbiguousVarargsInvocation=true -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.
management.jmxremote.authenticate=false
6456 Bootstrap -Djava.util.logging.config.file=E:\V6.0\tomcat8\bin\..\conf\logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -
Xms2G -Xmx4G -Xss1m -XX:NewSize=1G -XX:MaxNewSize=1G -XX:MaxMetaspaceSize=2G -XX:+UseConcMarkSweepGC -Dcom.sun.management.jmxremote -DA8.datasource.properies.fil
epath=E:\V6.0\tomcat8\bin\..\../base/conf/datasourceCtp.properties -Dsun.rmi.dgc.client.gcInterval=21600000 -Dsun.rmi.dgc.server.gcInterval=21600000 -XX:+HeapDum
pOnOutOfMemoryError -Dsun.zip.disableMemoryMapping=true -Dorg.apache.el.parser.COERCE_TO_ZERO=true -Djgroups.bind_addr=0.0.0.0 -DDEE_HOME=E:\V6.0\tomcat8\bin\..\
../base/dee/ -Djgroups.tcpping.initial_hosts= -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Xdebug -Xnoagent -
Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8787 -Dignore.endorsed.dirs= -Dcatalina.base=E:\V6.0\tomcat8\bin\.. -Dcatalina.home=
E:\V6.0\tomcat8\bin\.. -Djava.io.tmpdir=E:\V6.0\tomcat8\bin\..\temp
8860 Jps -Denv.class.path=D:\Program Files (x86)\jdk1.8.0_77\lib\dt.jar;D:\Program Files (x86)\jdk1.8.0_77\lib\tools.jar;D:\Program Files (x86)\apache-ant-1.9.4\
lib -Dapplication.home=D:\Program Files (x86)\jdk1.8.0_77 -Xms8m
三、原始碼詳解
可以在github上直接下載javaDev的原始碼,找到sun.tools.jps.Jps類和sun.tools.jps.Arguments。
1 . sun.tools.jps.Arguments解析args引數部分
public Arguments(String[] args) throws IllegalArgumentException {//程式碼邏輯已經很清楚了,不再做詳細說明
int argc = 0;
if (args.length == 1) {
if ((args[0].compareTo("-?") == 0)
|| (args[0].compareTo("-help")== 0)) {
help = true;
return;
}
}
for (argc = 0; (argc < args.length) && (args[argc].startsWith("-"));
argc++) {
String arg = args[argc];
if (arg.compareTo("-q") == 0) {
quiet = true;
} else if (arg.startsWith("-")) {
for (int j = 1; j < arg.length(); j++) {
switch (arg.charAt(j)) {
case 'm':
mainArgs = true;
break;
case 'l':
longPaths = true;
break;
case 'v':
vmArgs = true;
break;
case 'V':
vmFlags = true;
break;
default:
throw new IllegalArgumentException("illegal argument: "
+ args[argc]);
}
}
} else {
throw new IllegalArgumentException("illegal argument: "
+ args[argc]);
}
}
switch (args.length - argc) {
case 0:
hostname = null;
break;
case 1:
hostname = args[args.length - 1];
break;
default:
throw new IllegalArgumentException("invalid argument count");
}
try {
hostId = new HostIdentifier(hostname);
} catch (URISyntaxException e) {
IllegalArgumentException iae =
new IllegalArgumentException("Malformed Host Identifier: "
+ hostname);
iae.initCause(e);
throw iae;
}
}
2 . sun.tools.jps.Jps列印資訊部分
//程式碼邏輯已經很清楚了,不再做詳細說明
Set<Integer> jvms = monitoredHost.activeVms();
for (Integer jvm: jvms) {
StringBuilder output = new StringBuilder();
Throwable lastError = null;
int lvmid = jvm;
output.append(String.valueOf(lvmid));
if (arguments.isQuiet()) {
System.out.println(output);
continue;
}
MonitoredVm vm = null;
String vmidString = "//" + lvmid + "?mode=r";
String errorString = null;
try {
// Note: The VM associated with the current VM id may
// no longer be running so these queries may fail. We
// already added the VM id to the output stream above.
// If one of the queries fails, then we try to add a
// reasonable message to indicate that the requested
// info is not available.
errorString = " -- process information unavailable";
VmIdentifier id = new VmIdentifier(vmidString);
vm = monitoredHost.getMonitoredVm(id, 0);
errorString = " -- main class information unavailable";
output.append(" " + MonitoredVmUtil.mainClass(vm,
arguments.showLongPaths()));
if (arguments.showMainArgs()) {
errorString = " -- main args information unavailable";
String mainArgs = MonitoredVmUtil.mainArgs(vm);
if (mainArgs != null && mainArgs.length() > 0) {
output.append(" " + mainArgs);
}
}
if (arguments.showVmArgs()) {
errorString = " -- jvm args information unavailable";
String jvmArgs = MonitoredVmUtil.jvmArgs(vm);
if (jvmArgs != null && jvmArgs.length() > 0) {
output.append(" " + jvmArgs);
}
}
if (arguments.showVmFlags()) {
errorString = " -- jvm flags information unavailable";
String jvmFlags = MonitoredVmUtil.jvmFlags(vm);
if (jvmFlags != null && jvmFlags.length() > 0) {
output.append(" " + jvmFlags);
}
}
errorString = " -- detach failed";
monitoredHost.detach(vm);
System.out.println(output);
errorString = null;
}
四、後記
1 . 通過原始碼可知Jps使用的是RMI方式獲取JVM的資訊(只略知一二,後續自己還需補充學習);