How to Get Pid of Process I'Ve Just Started Within Java Program

How do I find the process ID (pid) of a process started in java?

This guy calls out to bash to get the PID. I'm not sure if there is an java solution to the problem.

/**
* Gets a string representing the pid of this program - Java VM
*/
public static String getPid() throws IOException,InterruptedException {

Vector<String> commands=new Vector<String>();
commands.add("/bin/bash");
commands.add("-c");
commands.add("echo $PPID");
ProcessBuilder pb=new ProcessBuilder(commands);

Process pr=pb.start();
pr.waitFor();
if (pr.exitValue()==0) {
BufferedReader outReader=new BufferedReader(new InputStreamReader(pr.getInputStream()));
return outReader.readLine().trim();
} else {
System.out.println("Error while getting PID");
return "";
}
}

Source:
http://www.coderanch.com/t/109334/Linux-UNIX/UNIX-process-ID-java-program

How can a Java program get its own process ID?

There exists no platform-independent way that can be guaranteed to work in all jvm implementations.
ManagementFactory.getRuntimeMXBean().getName() looks like the best (closest) solution, and typically includes the PID. It's short, and probably works in every implementation in wide use.

On linux+windows it returns a value like "12345@hostname" (12345 being the process id). Beware though that according to the docs, there are no guarantees about this value:

Returns the name representing the running Java virtual machine. The
returned name string can be any arbitrary string and a Java virtual
machine implementation can choose to embed platform-specific useful
information in the returned name string. Each running virtual machine
could have a different name.

In Java 9 the new process API can be used:

long pid = ProcessHandle.current().pid();

How to get the pid of a Java subprocess?

Until Java 8 (included), you have to use workarounds.

From Java 9 onwards, there is a new getPid method in the Process class.

How to get a process id of exe running through java program

i cant get the process id of the exe start through cmd.exe but my purpose was served by making the cmd.exe to wait till the child exe is running using following command

Process process = runTime.exec("cmd.exe /c start /wait abc.exe "+Id, null, new       File("D:/My"));

and i got the process id of the cmd.exe using jna-api

Getting PID of a process that has just started

The correct way to terminate a Tomcat instance is via its own shutdown command. You should not be thinking of processes, or PIDs, or kills, at all.

Get the PID for a process started by Eclipse

if you have java/bin in your path then you can use java ps tool:
jps -l

Output:
17623 com.intellij.idea.Main
29003 sun.tools.jps.Jps

The first column is process id.

How to get PID of java.lang.Process in linux by JAVA

Java 9

Java 9 introduces a number "nice" changes, one is the inclusion of the native PID of a Process - see Process#pid for more details

import java.io.IOException;
import java.io.InputStream;

public class Test {

public static void main(String[] args) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("/Applications/Xcode.app/Contents/MacOS/Xcode");
pb.redirectErrorStream(true);
Process p = pb.start();
// Yes, I'm a bad developer, but I just want to demonstrate
// the use of the PID method :/
new Thread(new Consumer(p.getInputStream())).start();
System.out.println("PID = " + p.pid());
p.waitFor();
System.out.println("Exit with " + p.exitValue());
}

public static class Consumer implements Runnable {
private InputStream is;

public Consumer(InputStream is) {
this.is = is;
}

@Override
public void run() {
try {
int value = -1;
while ((value = is.read()) != -1) {
// I'm ignoring it for brevity
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

}

You can also obtain a reference to the ProcessHandle for the Process via the Process#toHandle method, which is kind of nice



Related Topics



Leave a reply



Submit