/
Executable.java
89 lines (81 loc) · 3.37 KB
/
Executable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package net.ME1312.SubServers.Bungee.Host;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinNT;
import java.io.File;
import java.lang.reflect.Field;
/**
* Executable String Handler Class
*/
public class Executable {
private Executable() {}
/**
* Format a command to be executed
*
* @param gitbash Git Bash location (optional)
* @param exec Executable String
* @return
*/
public static String[] parse(String gitbash, String exec) {
String[] cmd;
if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
if (gitbash != null && (exec.startsWith("bash ") || exec.startsWith("sh ")))
exec = '"' + gitbash + ((gitbash.endsWith(File.separator))?"":File.separator) + "bin" + File.separatorChar + "sh.exe\" -lc \"" +
exec.replace("\\", "/\\").replace("\"", "\\\"").replace("^", "^^").replace("%", "^%").replace("&", "^&").replace("<", "^<").replace(">", "^>").replace("|", "^|") + '"';
cmd = new String[]{"cmd.exe", "/q", "/c", '"'+exec+'"'};
} else {
cmd = new String[]{"sh", "-lc", exec};
}
return cmd;
}
/**
* Get the PID of a currently running process
*
* @param process Process
* @return Process ID
*/
public static Long pid(Process process) {
if (process.isAlive()) {
try {
return (long) Process.class.getDeclaredMethod("pid").invoke(process);
} catch (Throwable ex) {
try {
if (process.getClass().getName().equals("java.lang.Win32Process") || process.getClass().getName().equals("java.lang.ProcessImpl")) {
Field f = process.getClass().getDeclaredField("handle");
f.setAccessible(true);
long handle = f.getLong(process);
f.setAccessible(false);
Kernel32 k32 = Kernel32.INSTANCE;
WinNT.HANDLE nt = new WinNT.HANDLE();
nt.setPointer(Pointer.createConstant(handle));
return (long) k32.GetProcessId(nt);
} else if (process.getClass().getName().equals("java.lang.UNIXProcess")) {
Field f = process.getClass().getDeclaredField("pid");
f.setAccessible(true);
Object response = f.get(process);
f.setAccessible(false);
if (response instanceof Number) return ((Number) response).longValue();
}
} catch (Throwable e) {}
}
}
return null;
}
/**
* Terminate a currently running process
*
* @param process Process
*/
public static void terminate(Process process) {
if (process.isAlive()) {
if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
Long pid = pid(process);
if (pid != null) try {
Process terminator = Runtime.getRuntime().exec(new String[]{"taskkill", "/T", "/F", "/PID", pid.toString()});
terminator.waitFor();
} catch (Throwable e) {}
}
if (process.isAlive()) process.destroyForcibly();
}
}
}