Re: how to prevent a jar executed only once in a machine




Steve W. Jackson wrote:
In article <1151688143.315164.248010@xxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"John_Woo" <john_woo@xxxxxxxxxx> wrote:

Hi,

If my.jar is a swing UI app (or just a simple app), I'm wondering if
it's possible or how, that
this my.jar can be executed only once (no multi-session) in same
machine <or in same folder>?

Can any one tell?

--
Thanks
John
Toronto

There are a couple of common tricks used for preventing multiple
instances of an app from running.

One is to have your app open a ServerSocket on a specific port, which
prevents any other application on the same network interface from doing
the same (it'll already be in use when the second attempt occurs). But
this isn't always a good technique, depending on the port and other
factors.

Another is to create a file opened for output (I use a FileOutputStream)
and then obtain an exclusive lock on it. When the app terminates, the
lock is freed. I close the FileOutputStream at normal termination set
the file to delete when the JVM exits. When a second instance launches,
it may already find the file (in the event of an abnormal termination),
but it can't get a lock if another instance is running. I only use this
technique for restricting an individual user, so that the file is in the
user's home directory, but it can be readily adapted for system-wide use.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama

Thanks lots Steve.
Let's talk about the second aproach further.

I know using:

f = new File("new file");
f.close();

to create a phycical file. but questions :

1. how to obtain an exclusive lock on it? from file API, I can only
see

setReadOnly()

2. if this app is terminated by unexpected way <like in unix, kill
-pid>, how to rm this file?

--
Thanks lots
John
Toronto

.



Relevant Pages

  • Re: Had to uninstall Safari
    ... Steve de Mena wrote: ... If by 'deep' you mean 'in a folder clearly named for the app in a folder ... One I use, Native Instruments "Battery", has files in: ... installation. ...
    (comp.sys.mac.advocacy)
  • Re: Microsoft is Dead
    ... Snit wrote: ... Because as far away from PS as the app might be, ... Steve figured out that a program that alters images is "closer to PS ... Photoshop link to a webpage was to show that the program on the page was ...
    (comp.sys.mac.advocacy)
  • Re: Registry ACL Modification
    ... am relatively new to the whole adding/modifying reg keys. ... "Steve Easton" wrote: ... >> I wrote an app that needs to add a few small strings in the registry. ... >> can download. ...
    (microsoft.public.vb.winapi)
  • Re: Microsoft is Dead
    ... Because as far away from PS as the app might be, ... Steve figured out that a program that alters images is "closer to PS ... Photoshop link to a webpage was to show that the program on the page was not ...
    (comp.sys.mac.advocacy)
  • Re: OTsort of but not really, Nvidia issue that affects all apps.
    ... it wasn't the app but a system wide issue. ... I'm testing here was the response: ... Thanks for the update, Steve, and for your diligence in following up ...
    (microsoft.public.vb.general.discussion)