-1

Is there a way to find which jvms are currently running, and get their PIDs? e.g. when launching JVisualvm, it shows all local jvms. How can I do that?

Background: I'm working on an instrumentation project, and I would like to attach an agent to another (unknown) java process. The idea is to display all currently running java processes, and let the user choose a process from that list. Thanks!

[edit] I do mean processes, not threads.

iGili
  • 823
  • 7
  • 18
  • I think its NOT. Since OP wants to have list of processes currently running instead if threads in JVM. – Ved Sep 06 '12 at 12:37
  • wouldnt it be better to let both communicate using sockets or pipes for example? – CloudyMarble Sep 06 '12 at 12:35
  • 1
    You could use [jps](http://docs.oracle.com/javase/1.5.0/docs/tooldocs/share/jps.html). It comes with the JDK. – Dave Sep 06 '12 at 12:35
  • 1
    You might need to use Runtime.exec() to run an external(OS) command for listing all the PID and probably filtering them. For doing that you can look at [this](http://www.ensta-paristech.fr/~diam/java/online/io/javazine.html). Would that work I'm not sure and it will depend what kind of information you can get from the PID listing. – Alex Botev Sep 06 '12 at 12:37

1 Answers1

1

You can use Runtime for that. This should give you a way ahead to proceed with.

Process p = Runtime.getRuntime().exec(COMMAND_TO_GET_PROCESS_DATA);

Ref

Ved
  • 8,577
  • 7
  • 36
  • 69