Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » File locking error while using Ant(FileHandler can't get lock from Ant in Galileo)
File locking error while using Ant [message #515313] Thu, 18 February 2010 08:21 Go to next message
Eclipse UserFriend
I have a Java project that runs just fine in Eclipse 3.5.1 using the "Run as Java application" option. However, when I execute the same program via an Ant script, I get a java.io.IOException. The error message says "Couldn't get lock for log\mylog.xml".

Looking at the Java code, the problem exists in FileHandler.class, specifically in this constructor:

public FileHandler(String pattern, int limit, int count, boolean append)

This is the code that is crashing:

int unique = -1;

for (;;) {

unique++;

if (unique > MAX_LOCKS) {

throw new IOException("Couldn't get lock for " + pattern);

}

It appears that I am exceeding MAX_LOCKS but I'm not sure what value MAX_LOCKS has, why I'd be exceeding that value, or how to prevent it from exceeding it.

Any idea why I'd get this behaviour in Ant but not when I run the program with the "Run as Java application" option?

For what it's worth, I ran this program successfully via Ant many times in previous versions of Eclipse, like 3.1. Also, I tried rebooting the computer and deleting all of the existing log and lck files in the log directory before trying the program again but it still behaves the same way: it messes up in Ant but runs fine when executed as a Java application outside of Ant.

Have I found a bug in Eclipse 3.5.1, specifically in the Ant code? If yes, what should I do about it? File a Bugzilla report? Upgrade Ant or Eclipse to a newer (or older?) version?

I am running on Windows XP.
--
Rhino
Re: File locking error while using Ant [message #515331 is a reply to message #515313] Thu, 18 February 2010 04:08 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

Hi

There's some piece of code missing here because if this was the entire
loop then it will always fail. There must be some other check lower in
the loop that is causing it to try again. It is that other check which
is determining the "number" of locks that are in place.


> int unique = -1;
>
> for (;;) {
>
> unique++;
>
> if (unique > MAX_LOCKS) {
>
> throw new IOException("Couldn't get lock for " + pattern);
>
> }
>

--
Thanks,
Rich Kulp
Re: File locking error while using Ant [message #515338 is a reply to message #515313] Thu, 18 February 2010 09:18 Go to previous messageGo to next message
Eclipse UserFriend
You're absolutely right; that snippet only represents the code immediately prior to where the crash took place. It is in java.util.logging.FileHandler. one of the standard Java classes, so I didn't bother to post the whole class since everyone can access this code just as readily as I can. (By the way, I'm using jdk1.6.0_18.)

I'm really not sure what to do here. As I said, the IO exception is happening in the FileHandler class, not my own code. My code works perfectly when run as a Java application but when I try to execute it in Ant, I get this locking error when FileHandler tries to get a lock so that it can set up my error log. Clearly, something isn't working the same way in Ant as it does when run as a Java application, even though the Ant script worked perfectly well in older versions of Eclipse.

I'm not sure how to resolve this so I thought I'd post here to get some suggestions.

--
Rhino
Re: File locking error while using Ant [message #515339 is a reply to message #515313] Thu, 18 February 2010 04:22 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

This reminds me of a problem I had quite some time back. I thought I had
posted a bug report at the time, but couldn't now seem to find it. IIRC,
what happened to me, then, was the following:

I had an ANT script that opened files and did something to them. Closing
the files was implicit upon the end of ANT respectively the end of the
particular Java program triggered from ANT. The same file could be
opened multiple times in one run of the same ANT script. This worked
fine when using ANT from command line, but failed to work when using ANT
from within Eclipse. The reason was that when using ANT from within
Eclipse, the implicit close doesn't happen until Eclipse terminates
(because as far as the VM and the OS are concerned this is the only
process that there is). That wouldn't necessarily be a problem, but
under Windows opened files automatically get a lock that is also
associated with the process, so they cannot be reopened. I cannot quite
remember what the fix was, if there was one, but I assume I must
eventually have figured out a way to make the file close explicit within
my work flow.

hth,

Steffen

On 18/02/2010 13:21, Reinhardt Christiansen wrote:
> I have a Java project that runs just fine in Eclipse 3.5.1 using the
> "Run as Java application" option. However, when I execute the same
> program via an Ant script, I get a java.io.IOException. The error
> message says "Couldn't get lock for log\mylog.xml".
> Looking at the Java code, the problem exists in FileHandler.class,
> specifically in this constructor:
>
> public FileHandler(String pattern, int limit, int count, boolean append)
>
> This is the code that is crashing:
>
> int unique = -1;
>
> for (;;) {
>
> unique++;
>
> if (unique > MAX_LOCKS) {
>
> throw new IOException("Couldn't get lock for " + pattern);
>
> }
>
> It appears that I am exceeding MAX_LOCKS but I'm not sure what value
> MAX_LOCKS has, why I'd be exceeding that value, or how to prevent it
> from exceeding it.
>
> Any idea why I'd get this behaviour in Ant but not when I run the
> program with the "Run as Java application" option?
>
> For what it's worth, I ran this program successfully via Ant many
> times in previous versions of Eclipse, like 3.1. Also, I tried
> rebooting the computer and deleting all of the existing log and lck
> files in the log directory before trying the program again but it
> still behaves the same way: it messes up in Ant but runs fine when
> executed as a Java application outside of Ant.
> Have I found a bug in Eclipse 3.5.1, specifically in the Ant code? If
> yes, what should I do about it? File a Bugzilla report? Upgrade Ant or
> Eclipse to a newer (or older?) version?
>
> I am running on Windows XP.
> --
> Rhino
Re: File locking error while using Ant [message #515375 is a reply to message #515339] Thu, 18 February 2010 10:38 Go to previous messageGo to next message
Eclipse UserFriend
Thanks for the suggestion, Steffen. It is possible that this is where my problem lies. Mind you, I have already shut down and reboot the computer and it still didn't solve the problem so, unless Windows keeps its lock even through a reboot, I'm not sure what more I can do to release the locks.

If anyone can confirm or refute Steffen's suggestion, I'd be delighted to hear from you. If you have a solution to my problem, I'd be even happier!

--
Rhino
Re: File locking error while using Ant [message #515479 is a reply to message #515338] Thu, 18 February 2010 16:23 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

The code indicates:

1) It will look for lockfiles ( lockFileName = generate(pattern, 0,
unique).toString() + ".lck"; ) whatever the lock file name pattern is
generated, it will try for up to 100 of these files and if they all
exist in the file system then you will get this error.

or

2) It has a static map of lock files, and if the request file, even if
not in the file system IS in this static then it will increment unique
and try again.

or

3) It tries to open for output this file, and if it can't then it will
increment unique and try again

or

4) It tries to actually lock the file, and if it can't then it will
increment unique and try again

------------------

So this means that some files are being locked and then never released
that use the given "pattern" for locking. They are released by having
the associated filehandler closed.

You didn't say if you are running this from Eclipse as an Ant launcher
or from a command line.

If you are running from a command line this means you are doing many
locks and no unlocking.

If you are running from Eclipse AND you are having run WITHIN Eclipse
instead of a separate JRE then it will be using the same JRE instance as
Eclipse is using and so if you don't unlock, even though the Ant script
has terminated, the file will stay locked because it is a static inside
the Eclipse JVM instance. So every time you run the Ant script the
number of locks would increase.

--
Thanks,
Rich Kulp
Re: File locking error while using Ant [message #515490 is a reply to message #515479] Thu, 18 February 2010 18:56 Go to previous messageGo to next message
Eclipse UserFriend
Thank you for that information, Rich.

I'm not sure if I entirely understand what you are saying. As a result of your words, I've rebooted the computer and deleted all existing log files - there were only 3, nowhere near to 100 - on the theory that a reboot would surely unlock any locked files. (Perhaps this is not correct?). I also changed the JRE in the Ant configuration settings to be a different one that my default in Eclipse. Now, Eclipse is using a default of 1.6.0_18 while Ant is using a 1.6.0_07 JRE. When I ran the Ant script again, I got the exact same error as before.

In answer to your question, I am running Ant from within Eclipse and NOT from the command line.

If I have indeed exceeded the number of locks I am allowed to have and rebooting doesn't clear those locks, what is the right way to close them?

Also, is there any way I can programmatically determine what locks exist, inspect them, and then close the ones that I want to close? Or is there some feature of Eclipse that will help me do this? I'm not sure what a 'static' is in this context and I certainly know very little about how Eclipse works internally, especially with regards to opening/closing files and locking/unlocking them.

Should my programs make a point of always manually unlocking files? If so, how do I do that? I had assumed that locking and unlocking was handled by Java or the OS and that I didn't need to bother with it so I'm not sure how to go about it.

If you can clarify these points for me, I think we'll be on the way to a solution....

--
Rhino
Re: File locking error while using Ant [message #515492 is a reply to message #515490] Thu, 18 February 2010 19:22 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

Hi,

In this particular case no I don't know. It's part of the logging code
and for some reason you are creating a bunch of locks and not releasing
them.


--
Thanks,
Rich Kulp
Re: File locking error while using Ant [message #515507 is a reply to message #515492] Fri, 19 February 2010 00:27 Go to previous messageGo to next message
Eclipse UserFriend
Would you agree that the symptoms seem to show that there is some kind of interaction between Eclipse and Ant (and maybe the operating system and JDK) that is causing this locking problem?

If so, what is the best way to handle it? Upgrade to a newer Eclipse or Ant in the hope that the bug is fixed in newer versions?

Or does my code need to be manually releasing locks somehow?

Should I be doing a bugzilla report for this problem?

--
Rhino
Re: File locking error while using Ant [message #515530 is a reply to message #515375] Fri, 19 February 2010 04:53 Go to previous messageGo to next message
Eclipse UserFriend
Basically, this is related to what Rich Kulp is suggesting in his
responses. Does your ANT script possibly try to open the same file more
than once (possibly from different actions)?

Steffen

On 18/02/2010 15:38, Reinhardt Christiansen wrote:
> Thanks for the suggestion, Steffen. It is possible that this is where
> my problem lies. Mind you, I have already shut down and reboot the
> computer and it still didn't solve the problem so, unless Windows
> keeps its lock even through a reboot, I'm not sure what more I can do
> to release the locks.
>
> If anyone can confirm or refute Steffen's suggestion, I'd be delighted
> to hear from you. If you have a solution to my problem, I'd be even
> happier!
>
> --
> Rhino
Re: File locking error while using Ant [message #515616 is a reply to message #515507] Fri, 19 February 2010 09:21 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

Hi,

You said you ran your app as a Java Application and it worked.

What did you do different to kick it off from Ant? What Ant Task did you
use to kick off your java app?

--
Thanks,
Rich Kulp
Re: File locking error while using Ant [message #515619 is a reply to message #515616] Fri, 19 February 2010 09:49 Go to previous messageGo to next message
Eclipse UserFriend
I use a java task in the Ant script to execute the program. Here is the code:

<java classname="com.foo.Baz" fork="false"
description="Execute Baz.">
<classpath>
<pathelement path="${common.bin}"/>
<pathelement path="${baz.bin}"/>
</classpath>
</java>

I am running Eclipse 3.5.1 and the Ant version is 1.7.1.

--
Rhino
Re: File locking error while using Ant [message #515629 is a reply to message #515619] Fri, 19 February 2010 09:59 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

Hi,

Do you call this multiple times within the Ant script, or is it called
only once?

This will execute in the same JVM as Ant, so if there are multiple calls
without releasing the locks between the calls, then they will build up
since these are static within the Ant VM.

Reinhardt Christiansen wrote:
> I use a java task in the Ant script to execute the program. Here is the
> code:
>
> <java classname="com.foo.Baz" fork="false"
> description="Execute Baz.">
> <classpath>
> <pathelement path="${common.bin}"/>
> <pathelement path="${baz.bin}"/>
> </classpath>
> </java>
>
> I am running Eclipse 3.5.1 and the Ant version is 1.7.1.
>
> --
> Rhino

--
Thanks,
Rich Kulp
Re: File locking error while using Ant [message #515651 is a reply to message #515629] Fri, 19 February 2010 11:09 Go to previous message
Eclipse UserFriend
I only execute the compile task once in the Ant script.

The main program instantiates a variety of classes and each of the classes that needs it, creates a log fle. In fact, the exact same physical file, i.e. a file having the exact same file name and stored in the same directory, is being created each time. I realize that is redundant but it never occurred to me until I started working through this problem with you since I'd never had a problem before running this program in Ant on my new Eclipse version.

However, despite that dubious design - I'm sure it would make more sense to create a single instance of the log file at the beginning and then simply share it with each class that needs it - the fact remains that I don't get this problem when I run the program as a Java application. Also, I get the lock error the very first time the class tries to open the log file but ONLY when I run the program via Ant, never when I run it as a Java application.

I'm putting together a bare-bones self-contained standalone example that illustrates the problem and it is experiencing the same problem. Would it be helpful to post this? That would enable others to try this and see if they get the same results.

--
Rhino
Previous Topic:There is a way to use ASTParser outside Eclipse?
Next Topic:How to install JDT from Eclipse?
Goto Forum:
  


Current Time: Wed Mar 26 01:36:34 EDT 2025

Powered by FUDForum. Page generated in 0.04439 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top