Martin
Martin

Reputation: 648

How does JPS tool get the name of the main class or jar it is executing

I am wondering, how does JPS tool get the name of the main class it is executed within jvm process. It is

jps -l
123   package.MainClass
456   /path/example.jar

I am talking specifically about Linux (I am not interested in Windows, and I have no Win machine to experiment on).

I could think of 2 ways

  1. Connecting to the JVM in question which in turn tells it
  2. From /proc file system

Regarding the first alternative, is it using local JMX connection? Still, it must go to /proc for the pids.

  1. There is PID, so it must ask OS anyway
  2. jps lists also itself

Regarding the second alternative, I feel this could be the correct one, because

  1. On the command line, there is either -jar or MainClass
  2. /proc knows wery well the PID
  3. Before jps starts doind something, it has own folder in /proc

But, I am facing little problem here. When java command is very long (e.g. there is extremely long -classpath parameter), the information about the command line does not fit into space reserved for it in /proc. My system has 4kB for it, and what I learned elsewhere, this is hardwired in OS code (changing it requires kernel compilation). However, even in this case jps is still able to get that main class somewhere. How?

I need to find quicker way to get JVM process than calling jps. When system is quite loaded (e.g. when number of JVMs start), jps got stuck for several seconds (I have seen it waiting for ~30s).

Upvotes: 1

Views: 2815

Answers (1)

apangin
apangin

Reputation: 98334

jps scans through /tmp/hsperfdata_<username>/<pid> files that contain monitors and counters of running JVMs. The monitor named sun.rt.javaCommand contains the string you are looking for.

To find out the format of PerfData file you'll have to look into JDK source code.

Upvotes: 4

Related Questions