jProcesses:使用Java獲取跨平臺程式的詳細資訊

banq發表於2022-03-07

使用 Java 獲取跨平臺程式詳細資訊:

    List<ProcessInfo> processesList = JProcesses.getProcessList();
    
    for (final ProcessInfo processInfo : processesList) {
        System.out.println("Process PID: " + processInfo.getPid());
        System.out.println("Process Name: " + processInfo.getName());
        System.out.println("Process Time: " + processInfo.getTime());
        System.out.println("User: " + processInfo.getUser());
        System.out.println("Virtual Memory: " + processInfo.getVirtualMemory());
        System.out.println("Physical Memory: " + processInfo.getPhysicalMemory());
        System.out.println("CPU usage: " + processInfo.getCpuUsage());
        System.out.println("Start Time: " + processInfo.getStartTime());
        System.out.println("Priority: " + processInfo.getPriority());
        System.out.println("Full command: " + processInfo.getCommand());
        System.out.println("------------------");
    }


根據程式ID殺死程式:

    boolean success = JProcesses.killProcess(3844).isSuccess();

 
更改程式優先順序
Unix/Mac:
    boolean ok = JProcesses.changePriority(3844, 5).isSuccess();
Windows:
    boolean ok = JProcesses.changePriority(3844, WindowsPriority.HIGH).isSuccess();

本專案分支:https://github.com/Osiris-Team/jProcesses2,附加功能和增強功能:

class Example{
    public static void main(String[] args) {
        ProcessUtils processUtils = new ProcessUtils();
        List<JProcess> list = processUtils.getProcesses();
        for (JProcess process : list) {
            // Collect process details:
            System.out.println(process.name);
            System.out.println(process.pid);
            System.out.println(process.command);
            System.out.println(process.usedMemoryInKB);
            // etc...

            // Parent/Child processes:
            JProcess parent = process.parentProcess;
            List<JProcess> childProcesses = process.childProcesses;

            // Cross-platform priorities and timestamps: 
            JProcessPriority priority = process.getPriority();
            Date timestampStart = process.getTimestampStart();

            // Interact with the process:
            process.changePriority(JProcessPriority.REAL_TIME);
            process.stop();
            process.kill();
        }

        // Easy access to the running JVM process:
        JProcess thisProcess = processUtils.getThisProcess(list);

        // Print parent/child processes in a tree:
        processUtils.printTree(list);
    }
}



 

相關文章