This discussion is archived
5 Replies Latest reply: Jul 6, 2008 5:03 PM by 648606 RSS

Need to use Java APIs to tell whether a file is locked

570033 Newbie
Currently Being Moderated
I have a script that automatically deletes files and folders from a workspace. I am receiving exceptions from items that are locked. I am not sure what attributes to request, how to request them, and what values to look for or compare them to in order to tell whether a file is locked. If someone could just tell me what attributes to request and how to compare them, that would be very helpful.

Thank you,

Dustin
  • 1. Re: Need to use Java APIs to tell whether a file is locked
    Pedja Expert
    Currently Being Moderated
    I didn't work with locks, but you shoud probably use Attributes.LOCKS on a document, it returns an Item array of locks so if it comes back empty the file isn't locked in any way (probably, I didn't work with it but it should work in that way). Look at API docs for other attributes that you can get from each lock.
    Try this code:
    AttributeRequest[] ar = new AttributeRequest[]{
      new AttributeRequest(Attributes.LOCKS, null)
    };
    Item file = commonManager.getItem(file.getId(), ar);
    NamedValue[] nv = file.getRequestedAttributes();
    Item[] locks = (Item[])nv[0].getValue();
    boolean fileLocked;
    if(locks!=null) fileLocked = true;
  • 2. Re: Need to use Java APIs to tell whether a file is locked
    570033 Newbie
    Currently Being Moderated
    I didn't see the LOCKS specific attribute, but you have pointed me in the right direction and I am pretty sure I will be able to get it working now.

    Thank you,

    Dustin
  • 3. Re: Need to use Java APIs to tell whether a file is locked
    603510 Newbie
    Currently Being Moderated
    Hi,
    I am interested if you managed a way.
    I am actually not using Oracle, but with a standard Java program, I need to delete a file. The problem is that File.delete() java method doesn't throw an exception but only returns a boolean. So how do I know what is the reason of the delete failure?
    Before trying to delete my file I would like to loop until the file is not locked anymore. Furthermore, if I then use a FileLock and lock my file, can I still delete it?
    Regards,
    Julien
  • 4. Re: Need to use Java APIs to tell whether a file is locked
    648606 Newbie
    Currently Being Moderated
    Hi,
    I just wanted to post some of my experience on java.nio.channels.FileLock.

    If a file is in use by an application, for example if a jar file is launched, the file is obviously locked by the system in a way we can't delete it.
    However it is still considered as unlocked by java as an exclusive FileLock could be set.

    If an exclusive FileLock has been set on a file no other exclusive FileLock could be set by any other application. (relieved ! the basic lock operation works !)

    When a FileLock is set on a jar file it DEFINITELY corrupt the considered jar !!!!!
    the message "Invalid or corrupt jarfile" is displayed when I try to execute it using java
    and any zip utility can't open it any more.
    And it is not a locking problem !
    Even after the exit of the application that lock the file, release of the lock, copy of the considered jar, the jarfile is definitely corrupted.
    I guess it could/should be the same thing on other file types.
    What an awful bug :(
    My config is Windows Vista sp1
    java version "1.6.0_05"
    Java(TM) SE Runtime Environment (build 1.6.0_05-b13)
    Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)

    Considering the question "if I then use a FileLock and lock my file, can I still delete it?"
    The answer is no you can't delete it:
    You can't delete it with the application that locked it
    (not even with File.deleteOnExit() method -- still with the above config --)
    neither with another application.
    Of course you can release the lock and delete it ...