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

后端 未结 14 1535
抹茶落季
抹茶落季 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:50

    The below program will be compatible with Java 9+ version only...

    To get the CurrentProcess information,

    public class CurrentProcess {
        public static void main(String[] args) {
            ProcessHandle handle = ProcessHandle.current();
            System.out.println("Current Running Process Id: "+handle.pid());
            ProcessHandle.Info info = handle.info();
            System.out.println("ProcessHandle.Info : "+info);
        }
    }
    

    For all running processes,

    import java.util.List;
    import java.util.stream.Collectors;
    
    public class AllProcesses {
        public static void main(String[] args) {
            ProcessHandle.allProcesses().forEach(processHandle -> {
                System.out.println(processHandle.pid()+" "+processHandle.info());
            });
        }
    }
    

提交回复
热议问题