- public class ProccessUtil {
- private static Logger logger = Logger.getLogger(ProccessUtil.class);
- static interface Kernel32 extends Library {
- public static Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
- public int GetProcessId(Long hProcess);
- }
- public static int getPid(Process p) {
- Field f;
- if (Platform.isWindows()) {
- try {
- f = p.getClass().getDeclaredField("handle");
- f.setAccessible(true);
- int pid = Kernel32.INSTANCE.GetProcessId((Long) f.get(p));
- return pid;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- } else if (Platform.isLinux()) {
- try {
- f = p.getClass().getDeclaredField("pid");
- f.setAccessible(true);
- int pid = (Integer) f.get(p);
- return pid;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- } else {
- }
- return 0;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/060620149722.html
来源: http://www.codesnippet.cn/detail/060620149722.html