Java Glossary : single instance

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You are here : home : Java Glossary : S words : single instance.

single instance
You may want to prevent the user from spawning multiple copies of your Java application. Here are two approaches:
  1. Test for the presence of a busy file. If one exists, abort. If not create one. The test and create can be in the bat file that triggers the app or in the app itself. When the app exists it deletes the busy marker file. The main problem with this simple approach is if your app crashes, it won't delete the busy file. You have to manually delete it before you can run the app again.
  2. Another approach is to have your application open a ServerSocket on a particular port number. The OS will prevent other processes from opening a ServerSocket that uses the same port. If you start the application and are unable to open your ServerSocket (i.e. if you get an "address already in use" exception) assume that the application is already running. In that case, you can use a Socket to connect to the running application and pass it whatever commands you like, or just abort.


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 1011.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/singleinstance.html J:\mindprod\jgloss\singleinstance.html