3

I am working on a java based desktop application. One requirement is that if application is running and user try to start the application again it should do something like show some alert etc.

I used file for this, when user run the app it add some text into a file and save it somewhere on the local system and when user try to run the app again i check the file and handle the situation accordingly, but there are some issue with this, like if application is abnormally terminated i am not able to remove that file and in that case when user tries to run the app system shows alert that application is already running but actually app terminated abnormally.

I did some R&D and found that i can read windows task manager list and then i can get and check my app if it is running. it was working fine but during testing i got an issue i-e there is a software/utility in windows named "tasklist" go to "start --> run" and type tasklist it will show you the task list. if this utility is not installed on your system or removed/corrupted for some reason then this will not work and same issue occur that user can start the application more then one time. Here is code to access the taskmanager items

Process p = Runtime.getRuntime().exec(
                System.getenv("windir") + "/system32/" + "tasklist.exe");

BufferedReader input = new BufferedReader(new InputStreamReader(
                p.getInputStream()));
while ((line = input.readLine()) != null) {
/*
Make the comparison here and show alert.
*/

}

Question: What is a good way to check if the app is already running? I am using Java for application, and advanced installer to create the installer of the application.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
NoNaMe
  • 6,020
  • 30
  • 82
  • 110
  • 1
    duplicate? http://stackoverflow.com/questions/660205/start-program-if-not-already-running-in-java – ddoor Jun 28 '13 at 11:38
  • Eclipse deals with a .lock file and the back-drawn is what you have in experience. "Desktop" doesn't mean Windows, would you implement for Linux, Maxosx and such? –  Jun 28 '13 at 11:41
  • I'll open a `FileInputStream` to a **lock** file. If open failed the app is running. Otherwise it's not. – johnchen902 Jun 28 '13 at 11:43

2 Answers2

5

Launch the app. with Java Web Start and use the SingleInstanceService. See the demo. of the SingleInstanceService for (demo. and) example code.

am using .. advanced installer to create the installer

Note that JWS:

  1. Is also an application installer. So I am suggesting this route as an alternative to using 'advanced installer'.
  2. Is provided by the makers of Java (Sun/Oracle).
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

Check out http://ss64.com/nt/wmic.html and try

String line;
try {
    Process proc = Runtime.getRuntime().exec("wmic.exe");
    BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    OutputStreamWriter oStream = new OutputStreamWriter(proc.getOutputStream());
    oStream .write("process where name='THE NAME OF YOUR PROCESS.exe'");
    oStream .flush();
    oStream .close();
    while ((line = input.readLine()) != null) {
        System.out.println(line);
    }: 
    input.close();
} catch (IOException ioe) {
    ioe.printStackTrace();
}

Courtesy:How to detect via Java whether a particular process is running under Windows?

Community
  • 1
  • 1
Vrashabh Irde
  • 14,129
  • 6
  • 51
  • 103