2

I have this somewhat unusual process structure:

  1. Launch4J starts my Java application. It creates a mutex to provide single-instance functionality for the Java application.
  2. The Java application starts a VB6 application, which can have multiple instances.
  3. When the Java application terminates, the VB6 application is still running. (Desired behaviour)

The problem is: The mutex created by Launch4J is only released after the VB6 application terminates. Because of that it's impossible to start the Java application again.

Why would this happen? I'm not opening the mutex explictly...

I first suspected it is because of Java using CreateProcess with bInheritHandles == true, but the problem does not occour when I start notepad.exe for example.

EDIT: I still have this problem. Any pointers are appreciated!

Daniel Rikowski
  • 71,375
  • 57
  • 251
  • 329
  • Did you have a look with ProcessExplorer that the mutant is still there? Which process owns the mutant? – Dirk Vollmar Sep 02 '09 at 07:01
  • Yes, it is there. And I can watch the reference count change when my VB6 app starts. But I'm using WinObj, which doesn't show the owner, because I couldn't find anything about mutexes in ProcessExplorer. – Daniel Rikowski Sep 02 '09 at 07:15
  • You can view all handles in ProcessExplorer via *View -> Lower Pane View -> Show Handles*. That will also show you the mutexes owned by the selected process. Or you can search by the mutex name (Ctrl + F). – Dirk Vollmar Sep 02 '09 at 07:19

3 Answers3

1

Does Launch4J release the mutex and close its handle before terminating? I'm sorry, but I don't know how Java wraps the OS Mutex functions, but you should ensure you explicitly release the mutex and close its handle before your thread ends.

pipTheGeek
  • 2,703
  • 17
  • 16
  • Launch4J creates a native Win32 wrapper, so it's not really Java related. Yes, the mutex reference count decreases as soon as the Java app terminates. – Daniel Rikowski Sep 03 '09 at 09:29
  • Sorry, I wasn't suggesting the problem was Java related, just apologising for not being able to tell you which methods to call. There is a difference between the Mutux reference count decrementing becuase it has been abandoned or becuase it has been released correctly, it is that difference I was trying to point out. – pipTheGeek Sep 06 '09 at 17:42
1

I faced the same kind of issue and realized that Launch4J creates an inheritable mutex on startup and when launching a process from withing the JVM, this mutex is then inherited by the new process.

After JVM shutdown the mutex is still held by the new process.

The simplest solution I found to avoid the mutex to be inherited is to use an intermediate program that launches a process as a detached process without inheriting the parent handles.

A working c++ example of this program can be found here https://stackoverflow.com/a/1582197/6894604

Just compile the program (“ex: rujob.exe”) using a c++ compiler and change your command to use the launcher instead of directly calling the process, for example :

new ProcessBuilder().command(
           "runjob.exe", 
           "vbprogram.exe", 
           "/PARAM1", 
           "/PARAM2").start();

This way your VB program will not inherit the java application mutex and will not prevent your java app to start again.

  • Good workaround, we are using `cmd /c` to achive the same effect. Any idea why this doesn't happen when I start other applications, like notepad.exe for example? – Daniel Rikowski Dec 13 '20 at 12:30
  • Not sure since on my environment the same problem occurs if I launch notpad.exe and the workaround using `cmd /c` command did not worked either, the solution with a separate launcher was the only one working for me. I tried with the simplest example with Java 1.8 & JavaFx on Win10. The mutex is first held by javaw.exe is systematically inherited by child processes. – Kununicckurra Dec 14 '20 at 08:56
0

Why don't you provide single instance functionality using VB instead of java? IMO it is wrong to provide single instance for the VB app using Launch4j. There are other ways, check this:

https://www.vbforums.com/showthread.php?342810-Classic-VB-How-can-I-allow-only-one-instance-of-my-application-to-run-at-a-time

vezenkov
  • 4,009
  • 1
  • 26
  • 27
  • The question might have been misleading. The single-instance functionality is solely required and implemented for the Java app. I added some clarification. – Daniel Rikowski Dec 13 '20 at 12:32