How to get a list of current open windows/process with Java?

后端 未结 14 1597
抹茶落季
抹茶落季 2020-11-22 05:20

Does any one know how do I get the current open windows or process of a local machine using Java?

What I\'m trying to do is: list the current open task, windows or

14条回答
  •  眼角桃花
    2020-11-22 05:48

    On Windows there is an alternative using JNA:

    import com.sun.jna.Native;
    import com.sun.jna.platform.win32.*;
    import com.sun.jna.win32.W32APIOptions;
    
    public class ProcessList {
    
        public static void main(String[] args) {
            WinNT winNT = (WinNT) Native.loadLibrary(WinNT.class, W32APIOptions.UNICODE_OPTIONS);
    
            WinNT.HANDLE snapshot = winNT.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
    
            Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();
    
            while (winNT.Process32Next(snapshot, processEntry)) {
                System.out.println(processEntry.th32ProcessID + "\t" + Native.toString(processEntry.szExeFile));
            }
    
            winNT.CloseHandle(snapshot);
        }
    }
    

提交回复
热议问题