Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get running process information in java?

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?

like image 575
dtd Avatar asked Dec 04 '25 04:12

dtd


1 Answers

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")));
like image 125
Adam Avatar answered Dec 05 '25 21:12

Adam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!