| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.dev.util; |
| 7 |
|
| 8 |
import java.io.ByteArrayOutputStream; |
| 9 |
import java.io.InputStream; |
| 10 |
import java.io.OutputStream; |
| 11 |
import java.nio.charset.StandardCharsets; |
| 12 |
import java.util.Map.Entry; |
| 13 |
import java.util.TreeMap; |
| 14 |
|
| 15 |
/** |
| 16 |
* Allows to kill a certain Java process. |
| 17 |
*/ |
| 18 |
public class JavaProcessKiller { |
| 19 |
|
| 20 |
/** |
| 21 |
* Kill a certain Java process. The JDK (jps) needs to be in the path. |
| 22 |
* |
| 23 |
* @param args the Java process name as listed by jps -l. If not set the |
| 24 |
* Java processes are listed |
| 25 |
*/ |
| 26 |
|
| 27 |
public static void main(String... args) { |
| 28 |
new JavaProcessKiller().run(args); |
| 29 |
} |
| 30 |
|
| 31 |
private void run(String... args) { |
| 32 |
TreeMap<Integer, String> map = getProcesses(); |
| 33 |
System.out.println("Processes:"); |
| 34 |
System.out.println(map); |
| 35 |
if (args.length == 0) { |
| 36 |
System.out.println("Kill a Java process"); |
| 37 |
System.out.println("Usage: java " + getClass().getName() + " <name>"); |
| 38 |
return; |
| 39 |
} |
| 40 |
String processName = args[0]; |
| 41 |
int killCount = 0; |
| 42 |
for (Entry<Integer, String> e : map.entrySet()) { |
| 43 |
String name = e.getValue(); |
| 44 |
if (name.equals(processName)) { |
| 45 |
int pid = e.getKey(); |
| 46 |
System.out.println("Killing pid " + pid + "..."); |
| 47 |
// Windows |
| 48 |
try { |
| 49 |
exec("taskkill", "/pid", "" + pid, "/f"); |
| 50 |
} catch (Exception e2) { |
| 51 |
// ignore |
| 52 |
} |
| 53 |
// Unix |
| 54 |
try { |
| 55 |
exec("kill", "-9", "" + pid); |
| 56 |
} catch (Exception e2) { |
| 57 |
// ignore |
| 58 |
} |
| 59 |
System.out.println("done."); |
| 60 |
killCount++; |
| 61 |
} |
| 62 |
} |
| 63 |
if (killCount == 0) { |
| 64 |
System.out.println("Process " + processName + " not found"); |
| 65 |
} |
| 66 |
map = getProcesses(); |
| 67 |
System.out.println("Processes now:"); |
| 68 |
System.out.println(map); |
| 69 |
} |
| 70 |
|
| 71 |
private static TreeMap<Integer, String> getProcesses() { |
| 72 |
String processList = exec("jps", "-l"); |
| 73 |
String[] processes = processList.split("\n"); |
| 74 |
TreeMap<Integer, String> map = new TreeMap<>(); |
| 75 |
for (int i = 0; i < processes.length; i++) { |
| 76 |
String p = processes[i].trim(); |
| 77 |
int idx = p.indexOf(' '); |
| 78 |
if (idx > 0) { |
| 79 |
int pid = Integer.parseInt(p.substring(0, idx)); |
| 80 |
String n = p.substring(idx + 1); |
| 81 |
map.put(pid, n); |
| 82 |
} |
| 83 |
} |
| 84 |
return map; |
| 85 |
} |
| 86 |
|
| 87 |
private static String exec(String... args) { |
| 88 |
ByteArrayOutputStream err = new ByteArrayOutputStream(); |
| 89 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 90 |
try { |
| 91 |
Process p = Runtime.getRuntime().exec(args); |
| 92 |
copyInThread(p.getInputStream(), out); |
| 93 |
copyInThread(p.getErrorStream(), err); |
| 94 |
p.waitFor(); |
| 95 |
String e = new String(err.toByteArray(), StandardCharsets.UTF_8); |
| 96 |
if (e.length() > 0) { |
| 97 |
throw new RuntimeException(e); |
| 98 |
} |
| 99 |
String output = new String(out.toByteArray(), StandardCharsets.UTF_8); |
| 100 |
return output; |
| 101 |
} catch (Exception e) { |
| 102 |
throw new RuntimeException(e); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
private static void copyInThread(final InputStream in, final OutputStream out) { |
| 107 |
new Thread("Stream copy") { |
| 108 |
@Override |
| 109 |
public void run() { |
| 110 |
byte[] buffer = new byte[4096]; |
| 111 |
try { |
| 112 |
while (true) { |
| 113 |
int len = in.read(buffer, 0, buffer.length); |
| 114 |
if (len < 0) { |
| 115 |
break; |
| 116 |
} |
| 117 |
out.write(buffer, 0, len); |
| 118 |
} |
| 119 |
} catch (Exception e) { |
| 120 |
throw new RuntimeException(e); |
| 121 |
} |
| 122 |
} |
| 123 |
}.start(); |
| 124 |
} |
| 125 |
|
| 126 |
} |