X-Received: by 10.68.222.167 with SMTP id qn7mr278824pbc.1.1358377749051; Wed, 16 Jan 2013 15:09:09 -0800 (PST) X-Received: by 10.50.220.231 with SMTP id pz7mr1116087igc.8.1358377749001; Wed, 16 Jan 2013 15:09:09 -0800 (PST) Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!ld4no3454766pbb.0!news-out.google.com!s9ni26pbb.0!nntp.google.com!f6no3425970pbd.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Wed, 16 Jan 2013 15:09:08 -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 23:09:09 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: csiph.com comp.lang.java.programmer:21452 On Wednesday, January 16, 2013 3:51:57 PM UTC-7, stle...@lanl.gov wrote: > On Tuesday, January 1, 2013 1:23:45 PM UTC-7, Roedy Green wrote: > What i= s the best way to ensure only a single instance of a Java program is runnin= g. I have used indicator files, but they can get screwed up if the user kil= ls the program without going through the standard shutdown. Ideally, net ne= w instance would just join the one already running. This is pure GUI, 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 th= eir 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 inst= ance of our software at a time. We had a configuration file that had initia= lization values for the software. If the file was missing, then the code ha= d default values. The code was running on one computer. Our code worked lik= e this: try{ Socket sock =3D new Socket(); sock.bind(new InetSocketAddress(= int value read from configuration file or default value)); } catch (java.ne= t.BindException) { tell user they are trying to start more than one instanc= e and shutdown this copy after user acknowledges } It's simple and seemed t= o always work. We only had to worry about a user double clicking the applic= ation. We did not worry about a batch file trying to start more than one in= stance. Obviously, you could start one instance, delete the configuration f= ile, then start the second instance. We judged this to be a very unlikely p= ossibility given our users, so we didn't worry about it. The software backe= d up its data on shutdown, and at regular intervals, and kept several backu= p files going back over the last week, so it was possible to restore the da= ta to a given time over the past week of operation. YMMV. John Oops! Need to catch an IOException, not a BindException!!