how to get running process information in java?

谁说我不能喝 提交于 2021-02-09 11:13:15

问题


i want to get infomation about other running processes in my os. (two things, process 'name' and 'path'.)

now, i'm using linux command like a "ps command".

Process process = Runtime.getRuntime().exec("ps x")

but because i want to run this in windows too, i'm searching other function can be works in windows and linux.

there are any java class or function have not os dependency?


回答1:


The updated Process API in Java 9 through JEP 102 will help you if you're willing to upgrade early... This provides platform agnostic access to process trees...

See ProcessHandle.allProcesses()

Example

ProcessHandle.allProcesses().forEach(processHandle -> 
    System.out.println("PID: " + processHandle.getPid() + 
    ", command: " + processHandle.info().command().orElse("Unknown")));


来源:https://stackoverflow.com/questions/43695086/how-to-get-running-process-information-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!