Java :Kill process runned by Runtime.getRuntime().exec() Java :Kill process runned by Runtime.getRuntime().exec() unix unix

Java :Kill process runned by Runtime.getRuntime().exec()


You can kill a sub-process that you have launched from your java application with destroy:

Process p = Runtime.getRuntime().exec("java -jar MyServerRunner -port MYPORT");p.destroy();

Also note that it might make sense to run that other code in a separate thread rather than in a separate process.


you can use .exec("ps|grep <your process name>");, and then parse the result to get the PID, finally .exec("kill PID");

Therefore, your process is killed but android app still alive.


You can get pid with reflection in unix (I know it is a bad idea :)) and call kill;

Process proc = Runtime.getRuntime().exec(   new String[] {"java","-classpath",System.getProperty("java.class.path"),... });Class<?> cProcessImpl = proc.getClass();Field fPid = cProcessImpl.getDeclaredField("pid");if (!fPid.isAccessible()) {    fPid.setAccessible(true);}Runtime.getRuntime().exec("kill -9 " + fPid.getInt(proc));