Reputation: 1653
is there a way to use java to detect which apps are running in a computer?I mean if I am running a browser,office app and a windows explorer ,can I use java to detect these and find how long they were running?
Upvotes: 0
Views: 444
Reputation: 719709
There is no pure Java way to do this.
The simplest way to do it is to use Runtime.exec(...)
to run an external command to list information on the commands currently running, and then unpick the resulting output stream. The command that you run (and its arguments) and the format of the result stream will be platform specific.
@Jigar has suggested using the "ps" command for Linux, and the "wmic" command for Windows. @Mahesh suggested "top" on Linux, but that doesn't show all applications ... and its output is not really designed for this purpose. Another possibility on Windows is "tasklist"; see @Jigar's linked page.
Another problem is deciding what constitute applications ... for the purposes of your listing. For instance, "ps" and "top" make no distinctions between different kinds of process. (As far as the Linux OS is concerned, there no real difference ...)
Finally, it would probably a bad idea to do this kind of thing in service that is exposed outside of your machine; e.g. in a Servlet or JSP. Letting other people see what is running on your server is likely to be a security risk.
Upvotes: 0
Reputation: 34665
If you running on a linux machine, top
call tells you running processes on the system. So, you can make a system call to it.
Runtime r = Runtime.getRuntime();
r.exec("top"); // The returned stream needs to be collected.
Upvotes: 0
Reputation: 234857
Use one of the Runtime.exec
methods to run whatever shell command would get you the info you want.
Upvotes: 0
Reputation: 240996
Execute following commands from java
Linux :
ps aux | less
Windows :
wmic process get description,executablepath
See Also
Upvotes: 2