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 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));