Detecting a Windows Process using Java

I need to write a java program to test if different processes are currently running on Windows, such as a Microsoft product. I may or may not want to start the process myself, but if it's already running do not want more than one process of the program running.  It can be a JNI solution (think it might have to be) or not .  I just something that will search for process name and get a boolean value back to say if it is running or not

Thanks
Ryan
ryno71Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WebstormCommented:
Hi ryno71,

You can use native code (.dll) using JNI (see the link posted by stmani2005) but if you have more than one java application running all process have the same name "java.exe" or "javaw.exe".

You can assign a port number to your application, and try to create a listenning socket like:
      serv_soc=new ServerSocket(TCP_PORT,10,InetAddress.getByName("127.0.0.1"));
if you get an exception, the port is already used (application already started).
0
ryno71Author Commented:
Actually I am just checking one application and not allowed to use a port, did think of that.. would work great :).  Thanks though.  Did find and look at the old post from 2002 interested in seeing the windows process dll   it spoke off.

ryno71
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Mayank SAssociate Director - Product EngineeringCommented:
>> if it's already running do not want more than one process of the program running

Is that supposed to be your own Java application? If so, you can use a file-lock approach to ensure that only one instance of the program runs.
0
Mayank SAssociate Director - Product EngineeringCommented:
>> You can assign a port number to your application, and try to create a listenning socket like

Yes, that is an approach too but if any other application wants to listen on the same port. Also, some ports are blocked after server hardening. A port should only be opened if data exchange is expected on it otherwise security and network teams in many organizations will not approve the project :)
0
ryno71Author Commented:
mayankeagle

Thats exactly why we can't do that way!  that and I don't want to receive all the phone calls

ryno71
0
Mayank SAssociate Director - Product EngineeringCommented:
You tried the file-lock?
0
ryno71Author Commented:
mayankeagle

I have thought of doing it that way with a file-lock (semaphore type of way)
, but was hoping for some type of unix (ps -ef) type of command for windows

ryno71
0
Mayank SAssociate Director - Product EngineeringCommented:
0
ryno71Author Commented:
mayankeagle

wish I could do it that way.. has to be like a ps -ef in unix.... but in windows, thanks though!

ryno71
0
Mayank SAssociate Director - Product EngineeringCommented:
Well, the file-lock method in Java is platform independent so it should work on Windows too. Not sure about ps -ef in Windows, check this:

http://www.sysinternals.com/Utilities/PsTools.html
0
ryno71Author Commented:
Don't mean to be a downer..  Asked about shareware too (PsTools especially).  I can't go this route either.

Ryno71
0
WebstormCommented:
If file lock doesn't work, then you can try File.delete() on an empty file: only one Process will get true from the delete() method. But you'll need to re-create this file when the application ends (possible if application is not aborted).
0
ryno71Author Commented:
I think I need to make a JNI call use the GetCurrentProcess() command from the Windows API... dont think there is any other way around it :(  .  Haven't done any JNI before.. Oh well

ryno71
0
ryno71Author Commented:
Could I also use process.h and just check to make sure we aren't using WINNT (psapi.dll problem)?
0
WebstormCommented:
>> Haven't done any JNI before
1) you create the Java source file declaring the native methods you want
    for example:
           public class SystemProcess {
               public native int getProcessIdByName(String name);
               static { System.loadLibrary("SystemProcess"); } // <-- SystemProcess.dll
           }

2) compile this file using javac as usual
3) use javah with the SystemProcess.class file generated, you'll obtain a SystemProcess.h file
4) implement the native method in SystemProcess.c file (dll project), using SystemProcess.h
0
Mayank SAssociate Director - Product EngineeringCommented:
BTW, you can use a JNI wrapper if you are not comfortable doing JNI and it will abstract out a lof of JNI stuff from you:

http://www.jniwrapper.com
0
ryno71Author Commented:
Is there any way of say setting a ProcessName to be able to find it later?  I am worried about the scenario where you have this process running on a server as admin and you have random people kicking it off.  Those type of scenarios will have their own process id and handles.. wont do me any good..  Wouldn't that be the case?



Ryno71
0
Mayank SAssociate Director - Product EngineeringCommented:
>> Is there any way of say setting a ProcessName to be able to find it later?

You want to change it? I doubt.
0
ryno71Author Commented:
I mean when you create/set it originally...  no after its already set up

Ryno71
0
Mayank SAssociate Director - Product EngineeringCommented:
I guess the process-name will be same as name of the program/ executable and the process-ID will be assigned by the OS.
0
ryno71Author Commented:
yes, but I am still wondering that if a another user from a different station logs in if I would see it.. don't think so.  Different login or a different machine loggin in different copy of the program...

ryno71
0
Mayank SAssociate Director - Product EngineeringCommented:
He might get to see it in the task manager under the appropriate user name under which the process is actually running.
0
ryno71Author Commented:
Unless its been changed to a service, but I think you would still see it
0
Mayank SAssociate Director - Product EngineeringCommented:
Yes. Or maybe you need admin rights to see the others :)
0
ryno71Author Commented:
I think you would yes..  
0
ryno71Author Commented:
You could use the following C code (Microsoft Sample) then use JNI

http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q175/0/30.ASP&NoWebContent=1#EZADAAA

another good example of the C code you could use is here

http://www.codeproject.com/threads/processes.asp

If someone did it differently,please feel free to let me know another way!

Thanks
ryno71
0
GranModCommented:
Closed, 500 points refunded.

GranMod
The Experts Exchange
Community Support Moderator of all Ages
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.