[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1184
  • Last Modified:

Determining if a Java Application is already running

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
circuit
Asked:
circuit
  • 2
  • 2
1 Solution
 
diakovCommented:
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
 
mrgentryCommented:
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
 
circuitAuthor Commented:
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
 
diakovCommented:
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
 
circuitAuthor Commented:
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

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now