I want to know which jars are loaded by all the different running JVM's.
If I type "lsof -p $PID | grep jar >> /somefile" from the bash/command, it works beautifully! (replacing $PID with an actual process id). However, I would like to be able to do this from within a Java program. I would expect the following code to work but no file gets written:
public static void printCustomCommand(){
String[] pids = {"pidof java"};
String s;
try {
Process pidProcess = Runtime.getRuntime().exec("pidof java");
BufferedReader br = new BufferedReader(new InputStreamReader(pidProcess.getInputStream()));
pids = br.readLine().split(" ");
for (String pid : pids){
String cmd = "lsof -p " + pid + " | grep jar >> /somepath/mydumpfile";
Process p;
p = Runtime.getRuntime().exec(cmd);
System.out.println(p.waitFor());
}
//pids = new String(bo).split(" ");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
When I print the p.waitFor() command, it returns a 1 always, meaning according to the API documentation "something is incorrect". http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html#waitFor%28%29