Use the following sample code to get the pid(process id) on *nix variants, after running it as a command from Java using Runtime.exec() .
The command variable, in my case, was running another Java application.
try {
Process process=Runtime.getRuntime().exec(new String [] {"sh","-c",command+" & echo $!"} );
BufferedReader br=new BufferedReader(new InputStreamReader(process.getInputStream()));String ps=br.readLine();
int pid=Integer.valueOf(ps);
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
Charles said,
September 4, 2009 at 1:26 pm
Valueworth command and I got salvated using this in the program
thanks for the same
mohanraj said,
January 13, 2010 at 7:17 am
i need java code for getting current running java as well as os process ID memory usage,performance and etc by using a java program