[关闭]
@boothsun 2022-01-26T05:28:40.000000Z 字数 2327 阅读 1240

jps命令笔记

JVM


jps介绍 ( JVM Process Status Tool )

网文

jps命令用于查看当前Java进程及其pid等相关信息,同ps -ef | grep java这种命令不同的是,jps并不使用应用程序名来查找JVM实例。因此,它查找所有的java应用程序,包括即使没有使用Java执行体的那种(例如定制的启动器)。另外,jps仅查找当前用户的Java进程,而不是当前系统中的所有进程。

我的观点:

  1. jps命令使用用来查看当前机器上Java进程以及这些进程对应的pid等相关信息。
  2. 其区别ps命令的点是:jps仅是查找当前用户的Java进程,而非其他用户的其他进程。

命令使用

  1. jps命令格式
    jps [options] [hostid]

  2. jps工具主要选项

选项 作用
-q 只输入LVMID,省略主类的名称
-m 输出虚拟机进程启动时传递给主类main()函数的参数。
-l 输出主类的全名,如果进程执行的是jar包,输出jar包路径。
-v 输出虚拟机进程启动时JVM参数

命令原理

Java程序在启动以后,会在java.io.tmpdir指定的目录下,就是临时文件夹里,生成一个类似于hsperfdata_User的文件夹;在这个文件夹里(在linux中为/tmp/hsperfdata_{username}),有几个文件,名字就是java进程的pid,因此列出当前运行的java进程,只是把这个目录里的文件名列一下而已。至于系统的参数什么,就可以解析这几个文件获得。
展示图片

1.每启动一个Java应用,其会在当前用户的临时目录下创建一个临时文件夹,以该应用的pid命名。

  1. public static final String dirNamePrefix = "hsperfdata_";
  2. public static String getTempDirectory(String user) {
  3. return tmpDirName + dirNamePrefix + user + File.separator;
  4. }

2.jps则会根据这些文件,获取本地的java进程,及具体的Main CLass 名称等。

  1. public synchronized Set<Integer> activeVms() {
  2. /*
  3. * This method is synchronized because the Matcher object used by
  4. * fileFilter is not safe for concurrent use, and this method is
  5. * called by multiple threads. Before this method was synchronized,
  6. * we'd see strange file names being matched by the matcher.
  7. */
  8. Set<Integer> jvmSet = new HashSet<Integer>();
  9. if (! tmpdir.isDirectory()) {
  10. return jvmSet;
  11. }
  12. if (userName == null) {
  13. /*
  14. * get a list of all of the user temporary directories and
  15. * iterate over the list to find any files within those directories.
  16. */
  17. File[] dirs = tmpdir.listFiles(userFilter);
  18. for (int i = 0 ; i < dirs.length; i ++) {
  19. if (!dirs[i].isDirectory()) {
  20. continue;
  21. }
  22. // get a list of files from the directory
  23. File[] files = dirs[i].listFiles(fileFilter);
  24. if (files != null) {
  25. for (int j = 0; j < files.length; j++) {
  26. if (files[j].isFile() && files[j].canRead()) {
  27. jvmSet.add(new Integer(
  28. PerfDataFile.getLocalVmId(files[j])));
  29. }
  30. }
  31. }
  32. }
  33. } else {
  34. /*
  35. * Check if the user directory can be accessed. Any of these
  36. * conditions may have asynchronously changed between subsequent
  37. * calls to this method.
  38. */
  39. // get the list of files from the specified user directory
  40. File[] files = tmpdir.listFiles(fileFilter);
  41. if (files != null) {
  42. for (int j = 0; j < files.length; j++) {
  43. if (files[j].isFile() && files[j].canRead()) {
  44. jvmSet.add(new Integer(
  45. PerfDataFile.getLocalVmId(files[j])));
  46. }
  47. }
  48. }
  49. }
  50. // look for any 1.4.1 files
  51. File[] files = tmpdir.listFiles(tmpFileFilter);
  52. if (files != null) {
  53. for (int j = 0; j < files.length; j++) {
  54. if (files[j].isFile() && files[j].canRead()) {
  55. jvmSet.add(new Integer(
  56. PerfDataFile.getLocalVmId(files[j])));
  57. }
  58. }
  59. }
  60. return jvmSet;
  61. }
  62. }

参考原文:

  1. 获取当前用户所有java进程及jps命令的实现
  2. Java系统工具jps原理解析
  3. Java命令学习系列(一)——Jps
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注