?
Solved

Determining if a Java Application is already running

Posted on 1999-01-26
5
Medium Priority
?
1,170 Views
Last Modified: 2013-11-23
Lots of applications I've written in C++ start out by checking to see if there's already an instance of the application running, and if so, bringing that instance's main window to the foreground, at which point, the second instance exits.

I want to have similar functionality in my Java Application (stand-alone app, not applet).

First, is there a pure-Java way to achieve this effect?
If not, I will increase the value of this question to 200 points if you have a good JNI native implementation.

One thought I had was to try and get a Process object for the current JavaVM process and store that as a desktopProperty on the default Toolkit. However, you can only get Process objects from the Runtime for sub-processes that you spawn via exec(). And, you can't call Toolkit.setDesktopProperty() because it's protected.

Thanks for your help. If your native code solution works, I'll increase the value of this question before I grade it.

bre++
0
Comment
Question by:circuit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:diakov
ID: 1234678
Let me elaborate.
I assume Win platform.

1. (pureJava solution) When you start your app, you can put a listening socket on a port on the local host. The next instance will first connect to this port to chech whether it has been started yet, and will exit if already running. Bringing the first instance to front, might be possible, the second program can tell the first one ot go to front through the socket, but I don't know how to do that pure java (bringing an Java app to the front), perhaps window1.toFront() on the main window will achieve this.

2. (JNI solution) Using the Win API, it is very easy to enumerate all windows, get their captions, check them and bring to focus. I have this code ready, it just have to be incorporated by the JNI and validated against Java windows.

3. You can use other flags, like temporary files in a temp directory, phrases in ini files, etc, etc, to mark the running application, so a second instance can find about it. This however constrains to second instance detection.

Hopoe this helps,
  Nik

0
 
LVL 1

Expert Comment

by:mrgentry
ID: 1234679
In a Win32 environment I believe the easiest (and apparently standard) way to tell if an application is already running is to have the application create a mutex (use some unique name for the mutex) and check for a return error of ERROR_ALREADY_EXISTS.

// Let's make sure only one copy of this executable can run at once.
HANDLE hMutex = CreateMutex(NULL, FALSE, "MyApp instance mutex");
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
       CloseHandle(hMutex);
       exit 1;
}

I think you'd probably have to use diakov's suggestion regarding finding the first instance's main window in order to bring it to the foreground.

Seems to me that diakov's suggestion for a pure java solution would work reasonable well, though I think you'll find that on some Windows machines you'll find that your application will cause dial-up networking to start.  I can't think of anything off the top of my head anyway that might be a better way to do it.

I wouldn't suggest using a temporary file to indicate the application is already running since if it ever fails to delete this file it would have to be removed manually to allow the application to run even a single instance again.

0
 

Author Comment

by:circuit
ID: 1234680
Nik, please just send a blank answer to this question and I'll give you the 100 points. I figured out my own native implementation.

Thanks again,
bre++
0
 
LVL 8

Accepted Solution

by:
diakov earned 400 total points
ID: 1234681
Thanx. Just in case, a small fragment of C++ code:
///-----------------
typedef struct
{
      BOOL found;
      HWND hwnd;
} EnumStruct;

BOOL CALLBACK EnumWindowsProc(
    HWND hwnd,      // handle to parent window
    LPARAM lParam       // application-defined value
   )
{
      char Title[100];
      EnumStruct *p = (EnumStruct *)lParam;

      if (hwnd != NULL)
            GetWindowText(hwnd, Title, 100);
      else
            return FALSE;
      if (strcmp(Title, STUB_NAME) == 0) //stub name is the //name of the window you're looking for.
      {
            p->hwnd = hwnd;
            p->found = TRUE;
            return FALSE;
      }
      return TRUE;
}

//Then in the initInstance
BOOL CStubApp::InitInstance()
{
      EnumStruct s;

      s.found = FALSE;
      s.hwnd = NULL;

      EnumWindows(EnumWindowsProc, (LPARAM)&s);
      if (s.found)
      {
            SetForegroundWindow(s.hwnd);
            return FALSE;
      }
//--------------
//...normall initialization and implementation.
}


<lock>
0
 

Author Comment

by:circuit
ID: 1234682
Thanks, here's my JNI implementation:


/*   CheckAppRunningImpl.cpp */

#include <jni.h>
#include "CheckAppRunning.h"
#include <stdio.h>
#include <windows.h>

JNIEXPORT jboolean JNICALL Java_CheckAppRunning_bringWindowToFront
  (JNIEnv *env, jobject obj, jstring windowName)
{
      HWND windowHandle;
      const char *str = env->GetStringUTFChars(windowName, JNI_FALSE);
      windowHandle = FindWindow ("SunAwtFrame", str);
      if (windowHandle)
      {
            SetForegroundWindow (windowHandle);
      }
      env->ReleaseStringUTFChars(windowName, str);
      if (windowHandle)
      {
            return JNI_TRUE;
      }
      else
      {
            return JNI_FALSE;
      }
}

JNIEXPORT jboolean JNICALL Java_CheckAppRunning_windowExists
 (JNIEnv *env, jobject obj, jstring windowName)
{
      HWND windowHandle;
      const char *str = env->GetStringUTFChars(windowName, JNI_FALSE);
      windowHandle = FindWindow ("SunAwtFrame", str);
      env->ReleaseStringUTFChars(windowName, str);

      if (windowHandle)
      {
            return JNI_TRUE;
      }
      else
      {
            return JNI_FALSE;
      }
}

/*   CheckAppRunning.java */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


class SimplePanel extends JPanel {

    public SimplePanel() {
      JButton button = new JButton("Hello, world");
        button.setMnemonic('h');

      add(button);
    }
}


public class CheckAppRunning {
    static JFrame frame;
    public native boolean windowExists(String windowName);
    public native boolean bringWindowToFront(String windowName);
   
    public static void main (String []args) {
      System.loadLibrary("CheckAppRunning");

      if (new CheckAppRunning().bringWindowToFront ("Check App Running")) {
            System.out.println ("App already running...exiting");
            System.exit(0);
      }
          
      SimplePanel panel = new SimplePanel();
      
      frame = new JFrame("Check App Running");
      frame.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {System.exit(0);}
      });
      frame.getContentPane().add("Center", panel);
      frame.pack();
      frame.setVisible(true);
    }
}


0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 10 hours left to enroll

752 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