Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe10.iad.POSTED!not-for-mail From: Daniel Pitts User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:17.0) Gecko/17.0 Thunderbird/17.0 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: single instance References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Lines: 23 Message-ID: <_8FFs.54988$LS5.54894@newsfe10.iad> X-Complaints-To: abuse@newsrazor.net NNTP-Posting-Date: Fri, 04 Jan 2013 18:26:02 UTC Date: Fri, 04 Jan 2013 10:26:01 -0800 X-Received-Bytes: 1769 Xref: csiph.com comp.lang.java.programmer:20946 On 1/1/13 12:23 PM, Roedy Green wrote: > What is the best way to ensure only a single instance of a Java > program is 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 GUI, so I am not worried about adding new command line > parms. > A common approach I've seen is to use an indicator file, which contains the PID of the main process. That way you can check if that PID is still active. This of course isn't platform independent. You could combine the idea with others in this thread though. Check the file, which contains a TCP port number, connect to that port number to check if the process is still alive. Just a thought. I might also have my program "watch" that file, and if it disappears or becomes modified, then shut down because I know the user is trying something they shouldn't.