Java tool/method to force-kill a child process

后端 未结 3 2024
长发绾君心
长发绾君心 2020-11-29 10:39

I am looking for a Java tool/package/library that will allow me to force-kill a child process.

This tool/package/library must work on Windows platform (mandatory).

3条回答
  •  温柔的废话
    2020-11-29 10:53

    For windows using jna 3.5.1

    try {
            Process p = Runtime.getRuntime.exec("notepad");
    
            Field f = p.getClass().getDeclaredField("handle");
            f.setAccessible(true);              
            long handLong = f.getLong(p);
    
                Kernel32 kernel = Kernel32.INSTANCE;
    
            WinNT.HANDLE handle = new WinNT.HANDLE();
    
            handle.setPointer(Pointer.createConstant(handLong));
    
            int pid = kernel.GetProcessId(handle);
    
            System.out.print(pid);
        } catch (Throwable e) {
    }
    

提交回复
热议问题