Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / April 2005

Tip: Looking for answers? Try searching our database.

Check if Java is already running

Thread view: 
Sebastian Scholz - 11 Apr 2005 08:58 GMT
Hi

I have a application where a second instance would run into problems, so I
like to inform the user, that the application is already running and
shuting down itself, if the user starts the application twice.

Any idea how to do this ?

-Sebastian
Arnaud Berger - 11 Apr 2005 09:10 GMT
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

Kurt Underhay - 11 Apr 2005 10:11 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.