This week's book giveaway is in the JDBC forum.
We're giving away four copies of Make it so: Java DB Connections & Transactions and have Marcho Behler on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes showing the OS processes(XP,Mac) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "showing the OS processes(XP,Mac)" Watch "showing the OS processes(XP,Mac)" New topic

showing the OS processes(XP,Mac)

Girish Vasmatkar
Ranch Hand

Joined: Apr 24, 2008
Posts: 201
Hello to all,

I know that this questioned has been asken an umpteen number of times and its solution is also available but here is the twist:

1. I want to list the processes from the other OS as well. Like we can execute tasklist.exe using PrecessBuilder class but how about showing processes running on a Mac machine or on a Linux machine for that matter.

2. Also I would like to go a step further in a sense that I want to have the feature "bring to front" in the task manager. That will bring the selected window to the front.

I guess that for my second question I'll have to use JNI. Can some body tell me how can I accomplist the task. Or should i ask this way- is it even possible?

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42959
For #1, the "ps" command is available on all Unix-based OSes. "ps a" includes all foreground applications, but it lists more processes than just those. So you may have to do some filtering of the list.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20202

As for 2), see SetWindowPos:
You can of course also add other flags, perhaps SWP_SHOWWINDOW.

How To Ask Questions How To Answer Questions
Girish Vasmatkar
Ranch Hand

Joined: Apr 24, 2008
Posts: 201
Thank you very much Ulf and Rob,

I'll try to use ps command and will let you know if I stuck any where.

For the second one, do I need to call that function using JNI? Also I will have to get the process ID of the process which is selected and then use that to bring the window related to that process to the front. How can I do that?
Rob Spoor

Joined: Oct 27, 2005
Posts: 20202

If you are only worried about the windows opened by your Java application, you can use Window.getWindows() to retrieve all windows, then use the toFront() method of Window.

If you want all windows present, then you'll have to go JNI again. The functions involved are:
- EnumWindows
- a callback function you wrote: BOOL CALLBACK XXX(HWND hwnd, LPARAM lParam) (the XXX is free to fill in)
- inside that callback function, IsWindowVisible to see if the window is not hidden and GetWindowText to get the caption / title

Edit: if you have the process ID, you can use the following instead of EnumWindows:
- GetCurrentThreadId
- EnumThreadWindows

There may be other ways I don't know of to get all threads of a process to replace the call to GetCurrentThreadId.

Edit 2: what was I smoking? GetCurrentThreadId is not going to help you. You'll need another way to get all threads of a process. I think this link can help you:
I agree. Here's the link:
subject: showing the OS processes(XP,Mac)
jQuery in Action, 3rd edition