[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Detecting a Windows Process using Java

Posted on 2006-03-22
29
Medium Priority
?
470 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:ryno71
  • 13
  • 10
  • 3
  • +2
28 Comments
 
LVL 13

Expert Comment

by:Webstorm
ID: 16259616
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
 

Author Comment

by:ryno71
ID: 16260431
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 30

Expert Comment

by:Mayank S
ID: 16266845
>> 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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16266870
>> 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
 

Author Comment

by:ryno71
ID: 16310373
mayankeagle

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

ryno71
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16310594
You tried the file-lock?
0
 

Author Comment

by:ryno71
ID: 16310798
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16310991
0
 

Author Comment

by:ryno71
ID: 16312183
mayankeagle

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

ryno71
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16318543
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
 

Author Comment

by:ryno71
ID: 16322201
Don't mean to be a downer..  Asked about shareware too (PsTools especially).  I can't go this route either.

Ryno71
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16325818
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
 

Author Comment

by:ryno71
ID: 16326196
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
 

Author Comment

by:ryno71
ID: 16327406
Could I also use process.h and just check to make sure we aren't using WINNT (psapi.dll problem)?
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16330871
>> 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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16330967
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
 

Author Comment

by:ryno71
ID: 16345813
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16349368
>> Is there any way of say setting a ProcessName to be able to find it later?

You want to change it? I doubt.
0
 

Author Comment

by:ryno71
ID: 16360689
I mean when you create/set it originally...  no after its already set up

Ryno71
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16360733
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
 

Author Comment

by:ryno71
ID: 16363228
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16367818
He might get to see it in the task manager under the appropriate user name under which the process is actually running.
0
 

Author Comment

by:ryno71
ID: 16370045
Unless its been changed to a service, but I think you would still see it
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16370088
Yes. Or maybe you need admin rights to see the others :)
0
 

Author Comment

by:ryno71
ID: 16370265
I think you would yes..  
0
 

Author Comment

by:ryno71
ID: 16386791
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
 

Accepted Solution

by:
GranMod earned 0 total points
ID: 16753368
Closed, 500 points refunded.

GranMod
The Experts Exchange
Community Support Moderator of all Ages
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question