ProcessUtil { public static void kill(Process process) { PROCESS_MANAGER.kill(process); } private  ProcessUtil(); static void process(Process p, LineConsumer stdout, LineConsumer stderr); static void process(Process p, LineConsumer stdout); static Process execute(ProcessBuilder pb, LineConsumer consumer); static boolean isAlive(Process process); static void kill(Process process); static int system(String command);  }
@Test public void testKill() throws Exception { final Process p = Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", "sleep 10; echo wake\\ up"}); final List<String> stdout = new ArrayList<>(); final List<String> stderr = new ArrayList<>(); final IOException[] processError = new IOException[1]; final CountDownLatch latch = new CountDownLatch(1); final long start = System.currentTimeMillis(); new Thread() { public void run() { try { ProcessUtil.process(p, new LineConsumer() { @Override public void writeLine(String line) throws IOException { stdout.add(line); } @Override public void close() throws IOException { } }, new LineConsumer() { @Override public void writeLine(String line) throws IOException { stderr.add(line); } @Override public void close() throws IOException { } } ); } catch (IOException e) { processError[0] = e; } finally { latch.countDown(); } } }.start(); Thread.sleep(1000); Assert.assertTrue(ProcessUtil.isAlive(p), "Process is not started."); ProcessUtil.kill(p); latch.await(15, TimeUnit.SECONDS); final long end = System.currentTimeMillis(); Assert.assertTrue((end - start) < 3000, "Fail kill process"); System.out.println(processError[0]); System.out.println(stdout); System.out.println(stderr); }