This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to check if an application is already running? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to check if an application is already running?" Watch "How to check if an application is already running?" New topic
Author

How to check if an application is already running?

olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
hi,

im asking myself how is it possible to detect if an instance of an application is already running.
i know i could create a .lock file which is created when the application starts, and, if another instance is created checks for that file (and aborts if it exists).
But this is imo a workaround and not a good way to do this.
Is there any way to check which applications are running in the JVM?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

With JNI you could check both the available windows and the running processes. Both have their drawbacks.
- Listing the available windows really does that - list the available windows. That includes any dialogs. Programs without any windows will not show up.
- Listing the running processes does not work well with Java, because the process executable is java.exe or javaw.exe. There is no way to make a distinction between two different programs both launched by a JVM.

That's why lock files or a server socket are often used in Java to find out if there already is another instance of the same program running. Native programs usually use the second technique which works fine because they each have their own application name. This is not fail safe either though since a) two different programs can share the same executable name, and b) by using a renamed copy of the executable this check can be prevented.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
when i google JNI i find nothing what could help me how to display running windows... can you give me more concrete informations?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

The Windows API call is called "EnumWindows".
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Rob Prime wrote:Native programs usually use the second technique which works fine because they each have their own application name.

There are quite a few native programs that still use the first technique, which can be a better solution.

For example: the Apache HTTPD server and MySQL and Postgres all use lock files (and this list is certainly not exhaustive).

IMHO this is a better technique as it allows the possibility of having multiple instances of the application using different resources. For example you might have an Apache web server on port 80 and a different one on port 81. Or perhaps more realistically a dev version of Tomcat (that can be killed if necessary) and a production version of Tomcat that is never killed - common resources can be used in both (I use this feature). Or you might have a dev database and a production database on the same physical machine, just on different ports and using different directory structures.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
i'll take a look at enumwindow, thanks!

There are quite a few native programs that still use the first technique, which can be a better solution.

But propably not in my case, there is just one instance per IP/PC allowed
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to check if an application is already running?
 
Similar Threads
Tomcat and service instances
Ensuring a Single Program Instance on Desktop
how to setup multile instances specific parameters of a same web application ?
got error while running weblogic from the unix environment
Tomcat instance creation problem