1

Just found this post (and found the code which I'm also pasting below):

java runtime.getruntime() getting output from executing a command line program

My question is, how do I kill the process? It seems that the code blocks in the while loop. I've tried several options like using a boolean, running all the code in a separate thread and stuff like this, but without any success. I just want to start an Android emulator and kill it whenever I want.

Runtime rt = Runtime.getRuntime();
String[] commands = {"emulator", "-avd", "jenkins", 
                    "-scale", "96dpi", "-dpi-device", "100"};
Process proc = rt.exec(commands);

BufferedReader stdInput = new BufferedReader(new 
     InputStreamReader(proc.getInputStream()));

BufferedReader stdError = new BufferedReader(new 
     InputStreamReader(proc.getErrorStream()));

// read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
    System.out.println(s);
}

// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
    System.out.println(s);
}
Community
  • 1
  • 1
noTest
  • 159
  • 1
  • 3
  • 12

1 Answers1

0

Okay.

Use below code to get The Process ID of that current running thread or Process.

  String processName =java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
  String ProcessID = processName.split("@")[0];//Process Id

Use that Process ID to kill that Process in your CPU.

I think for that purpose you may wish to write any other trigger or any condition in While loop.

Rookie007
  • 1,229
  • 2
  • 18
  • 50