how to avoid duplicated instance

E

Erica

I noticed when some application is open, if I click the same icon on
the desktop again, Windows will put the focus on the already opened
instance, instead of open an new one. Can any one tell me how to
accomplish this in Java? Your help will be greatly appreciated.
thanks.

-Erica.
 
J

Joona I Palaste

Erica said:
I noticed when some application is open, if I click the same icon on
the desktop again, Windows will put the focus on the already opened
instance, instead of open an new one. Can any one tell me how to
accomplish this in Java? Your help will be greatly appreciated.
thanks.

One very basic way would be to make your program take up a resource
(a file or a socket, for instance) when it opens and relieve it when it
closes. If your program finds this resource already occupied it quits
straight away.
Any more complex way would require delving into the Windows application
launching model, and that is outside Java's scope, although it can be
done with native code called from JNI.
 
R

Ricardo

Joona said:
One very basic way would be to make your program take up a resource
(a file or a socket, for instance) when it opens and relieve it when it
closes. If your program finds this resource already occupied it quits
straight away.
Any more complex way would require delving into the Windows application
launching model, and that is outside Java's scope, although it can be
done with native code called from JNI.

To add..

With the upcoming J2SE 1.5.0, a Single Instance Service will be part of
the JNLP API for deployment with Java Web Start.
 
J

Jon A. Cruz

Erica said:
I noticed when some application is open, if I click the same icon on
the desktop again, Windows will put the focus on the already opened
instance, instead of open an new one. Can any one tell me how to
accomplish this in Java? Your help will be greatly appreciated.
thanks.

One way could be to use Rendezvous/Zeroconf to broadcast and detect your
application.

There's even a free Java library you can use:

http://sourceforge.net/projects/jmdns/
 
E

Erica

Joona I Palaste said:
One very basic way would be to make your program take up a resource
(a file or a socket, for instance) when it opens and relieve it when it
closes. If your program finds this resource already occupied it quits
straight away.
Any more complex way would require delving into the Windows application
launching model, and that is outside Java's scope, although it can be
done with native code called from JNI.

I am able to check if any instance has been launched by checking a
socket. I just thought it would be nice to put the focus on the
already open application. Do you know where I can get more information
on windows application launching model? Thanks in advance.

-Erica
 
J

Jon A. Cruz

Erica said:
I am able to check if any instance has been launched by checking a
socket.

Maybe.

Unless you aren't allowed to grab that socket on one of the computers
you run on.

Or unless some other application grabs that socket and fools your app
into not even keeping a single instance.

I just thought it would be nice to put the focus on the
already open application. Do you know where I can get more information
on windows application launching model? Thanks in advance.

That'd be hard to say. Who's to say that more than one Java application
can't be run in a single Win32 JVM process?



That's why I'm now predisposed to the Zeroconf/Rendezvous solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top