问题
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