Hi,
You may want to use a listening ServerSocket on some port.
It is impossible to create more than one ServerSocket for one given port.
This will help knowing that one app is already running.
When the app launches, it may send a message (with a Socket object) to that
port to see if it responds.
If it does, just send a special message of yours (say "shutdown") on this
port, to tell the other app to shutdown gracefully.
Finally , let your app launch all of its stuff (including ServerSocket
listening on the port).
Regards,
Arnaud
> Hi
>
[quoted text clipped - 5 lines]
>
> -Sebastian
Sebastian Scholz - 11 Apr 2005 10:08 GMT
Thanks for the idea. I was thinking about a temp file but that could
remain if the application crashes. The socket times out in the worse case.
-Sebastian
> Hi,
>
[quoted text clipped - 23 lines]
>>
>> -Sebastian
Thomas Weidenfeller - 11 Apr 2005 10:46 GMT
> You may want to use a listening ServerSocket on some port.
> It is impossible to create more than one ServerSocket for one given port.
This was what I suggested in the past too. Until I realized that it will
fail on multi-user systems and can fail on multi-homed systems. Although
the later is unlikely.
On multi-user systems it prevents other users from running the same
application. This is fine if you want to have the application running
once per host, not once per user. But often you want to run it once per
user.
On multi-homed systems (machines with more than one IP address) you have
the same port multiple times, so you have to make sure all instances try
to bind to the same interface - which shouldn't be a problem.
I didn't like the file-locking attempt very much in the past, because
the existing locking mechanisms are shaky. File.createNewFile() is
unreliable (until and including 1.4 Sun said it is ok, since 1.4.1 Sun
says it is not ok for file locking ...). Nio's FileLock behavior is
highly implementation defined, but should work if used careful.
So my suggestion would be to try to use a FileLock, until something
better comes up.
/Thomas:wq

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
Steve W. Jackson - 12 Apr 2005 16:39 GMT
> > You may want to use a listening ServerSocket on some port.
> > It is impossible to create more than one ServerSocket for one given port.
[quoted text clipped - 22 lines]
>
> /Thomas:wq
I've successfully used the FileLock approach to limit an app to one
instance per user, and it works well on Windows, Linux and Mac OS X.
The OP mentioned something about a temp file getting left behind in the
event of a crash, but proper use of a FileLock handles that. I attempt
to create a file and ignore the possibility that it may exist already
(thus allowing that it may be a leftover). Then I attempt to lock it.
If the lock fails, then there's another instance running. If not, then
I free the lock when quitting, having already set the File to delete
when the VM shuts down normally.
= Steve =

Signature
Steve W. Jackson
Montgomery, Alabama
> Hi
>
[quoted text clipped - 5 lines]
>
> -Sebastian
One way of doing this is to use a lock-file: when the program starts up
it checks for the existence of the lock-file, if it doesn't exist it
knows its the only instance running; it creates the lock-file; on exit
it deletes it.