jProcesses:使用Java获取跨平台进程的详细信息

22-03-07 banq

使用 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);
    }
}



 

猜你喜欢