X-Received: by 10.68.226.168 with SMTP id rt8mr273185pbc.8.1358376717778; Wed, 16 Jan 2013 14:51:57 -0800 (PST) X-Received: by 10.50.154.132 with SMTP id vo4mr1090419igb.7.1358376717597; Wed, 16 Jan 2013 14:51:57 -0800 (PST) Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!news.ripco.com!rahul.net!wasp.rahul.net!rahul.net!news.kjsl.com!usenet.stanford.edu!ld4no3416905pbb.0!news-out.google.com!s9ni24pbb.0!nntp.google.com!ld4no3416904pbb.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Wed, 16 Jan 2013 14:51:57 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=132.3.53.68; posting-account=is92IAoAAAAyj17mr4XXQBDgnFK66iOH NNTP-Posting-Host: 132.3.53.68 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: single instance From: stledger@lanl.gov Cc: Roedy Green Injection-Date: Wed, 16 Jan 2013 22:51:57 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: csiph.com comp.lang.java.programmer:21451 On Tuesday, January 1, 2013 1:23:45 PM UTC-7, Roedy Green wrote: > What is the best way to ensure only a single instance of a Java program i= s running. I have used indicator files, but they can get screwed up if the = user kills the program without going through the standard shutdown. Ideally= , net new instance would just join the one already running. This is pure GU= I, so I am not worried about adding new command line parms. -- Roedy Green = Canadian Mind Products http://mindprod.com Students who hire or con others = to do their homework are as foolish as couch potatoes who hire others to go= to the gym for them. I worked on a project with a requirement to run only one instance of our so= ftware at a time. We had a configuration file that had initialization value= s for the software. If the file was missing, then the code had default valu= es. The code was running on one computer. Our code worked like this: try{ Socket sock =3D new Socket(); sock.bind(new InetSocketAddress(int value read from configuration file or d= efault value)); } catch (java.net.BindException) { tell user they are trying to start more than one instance and shutdown th= is copy after user acknowledges } It's simple and seemed to always work. We only had to worry about a user do= uble clicking the application. We did not worry about a batch file trying t= o start more than one instance. Obviously, you could start one instance, d= elete the configuration file, then start the second instance. We judged th= is to be a very unlikely possibility given our users, so we didn't worry ab= out it. The software backed up its data on shutdown, and at regular interva= ls, and kept several backup files going back over the last week, so it was = possible to restore the data to a given time over the past week of operatio= n. YMMV. John