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



In article <1151692705.444939.82070@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"John_Woo" <john_woo@xxxxxxxxxx> wrote:

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

I do something like this:

File homeDir = new File(System.getProperty("user.home"));
File f = new File(homeDir, "lockFileName");
f.createNewFile();

That creates the file, if it's not present, in the user's home
directory. But it doesn't care if the file's already present. (I'm
omitting try/catch blocks for brevity.)

Then I create a java.io.FileOutputStream around that file:

FileOutputStream fos = new FileOutputStream(f);

The file locking mechanism I use was added in Java 1.4. It's a
java.nio.channels.FileLock. You obtain one like this:

FileLock lock = fos.getChannel().tryLock();

If that succeeds, you've got a non-null FileLock object and an exclusive
lock on the output file. For good measure, I write a single byte to the
file. And I have to keep the FileOutputStream reference as long as my
application is running.

To get the JVM to delete the file automatically, I call:

f.deleteOnExit();

When my app is ready to terminate normally, I close the FileOutputStream
reference I've kept. If I don't, then the deleteOnExit() call has no
effect. If I do close it, then that file goes away automagically.

If your app terminates abnormally -- or you forget to close the output
stream -- then the file will already be there next time around, but that
wouldn't break this approach. The lock attempt will work fine again.
However, if a second instance of your app runs it'll be able to get the
file, but the tryLock() call will result in a null FileLock object when
it cannot lock the file. So the response would be to inform the user
that multiple instances aren't allowed and force a JVM shutdown.

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



Relevant Pages

  • Re: how to prevent a jar executed only once in a machine
    ... Steve W. Jackson wrote: ... If my.jar is a swing UI app, ... and then obtain an exclusive lock on it. ... If your app terminates abnormally -- or you forget to close the output ...
    (comp.lang.java.programmer)
  • Re: Lock problem - concurrent users
    ... Clipper 5.2 app at a client of mine. ... The app lives on a Win2003 Small Business server, ... Have you been able to determine if your lock routine fails to lock the ...
    (comp.lang.clipper)
  • Re: Execute on Focus
    ... then click the lock button. ... but it's VB6 code and I'd rather write my app in newer VB.net code. ... > Did you know that this is a VBNet newsgroup for some of us VB6 is more ...
    (microsoft.public.dotnet.languages.vb)
  • Re: vb application locks file
    ... But it seems you've narrowed it down to your own app. ... in releasing resources and you have evidently overlooked something. ... While possibly not associated with your current lock problem, ... The randomness or intermittent behavior also suggests that file i/o is ...
    (microsoft.public.vb.general.discussion)
  • How to read a partially locked file
    ... I'm having problems reading a partially locked file. ... The writer app (a 3rd ... My app tries to read the new data but since FileStream is ... The result is a lock exception when I try to read 1 byte that is within ...
    (microsoft.public.dotnet.framework)