How Can a Java Program Get Its Own Process Id

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 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 Java program on windows machine get a PID of a given process without using JNA

Finally I found a solution.
I have used wmic process get commandline, processid windows command to get the PID.

Following is my Killer.java :

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Killer {


public static void main(String[] args) throws Exception {



ArrayList<String> cmds = new ArrayList<String>();

cmds.add("wmic");
cmds.add("process");
cmds.add("get");
cmds.add("commandline,processid");

ProcessBuilder pb = new ProcessBuilder(cmds);

Process p = pb.start();

//p.waitFor();


BufferedReader rd = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line;
int pid=0;

while((line = rd.readLine()) != null)
{
if(line.contains(args[0]) && !line.contains("Killer"))
{

System.out.println("OK" + line);
String[] split = line.split(" ");
pid=Integer.parseInt(split[split.length - 1]);

}
else
{
//System.out.println(" " + line);
}
}

cmds = new ArrayList<String>();

System.out.println("Kill pid " + pid);
cmds.add("taskkill");
cmds.add("/T");
cmds.add("/F");
cmds.add("/PID");
cmds.add("" + pid);
pb = new ProcessBuilder(cmds);
pb.start();
}
}

Hope it will help you.

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



Related Topics



Leave a reply



Submit