This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to check if an application is already running?

 
olze oli
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
olze oli
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Windows API call is called "EnumWindows".
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11888
203
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
olze oli
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic