Problem with creating FileHandler

R

Rhino

Hi,

I am having an odd problem using Ant. Whenever my Java program tries to
create a new instance of java.util.logging.FileHandler, I get a
java.io.IOException that says "Couldn't get lock for log2\baz.log.xml".

The weird part of this problem is that I get this error when I am running
Ant but NOT when I simply execute the program in Eclipse.

I am running Eclipse 3.5.1 Galileo on Windows XP with SP 2. This version of
Eclipse contains Ant 1.7.1.

When I run the program from an Ant script within Eclipse OR from the Windows
command line (using Ant 1.8.0), I get the error that I mentioned. However,
when I run the program as a Java application from within Eclipse, it works
fine and does not generate this error.

I'm baffled and would appreciate some help to prevent this error.

I've created a simple example and an Ant build file that illustrates the
problem.

My project is called Logging and it is a regular Java project in Eclipse.
Within the package com.foo.baz2, which is in my src folder in the Logging
project, I have the following classes:

* Supremo, which is the driver program
* Alpha, which is the class invoked from Supremo which actually
writes to the log file

Here is the source code for Supremo:
================================
package com.foo.baz2;

import java.io.File;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Supremo {
/**
* The name of this class.
*/
private final String CLASS_NAME = getClass().getName();

/**
* The logger that will log error messages in this class.
*/
private Logger logger = null;
/**
* The file handler used by this logger.
*/
private Handler logFileHandler = null;

/**
* @param args
*/
public static void main(String[] args) {
new Supremo();
System.out.println("=== Supremo ends ===");
}
public Supremo() {
configureLogger(); new Alpha(logger).writeText();
}

/**
* Method configureLogger() configures the logger(s) used by this
program.
*
* <p>Additional comments about method configureLogger().</p>
*/
private final void configureLogger() {

String METHOD_NAME = "configureLogger()"; //$NON-NLS-1$
/* Create the logger. */ this.logger =
Logger.getLogger(CLASS_NAME);
/* Create a file handler for the logger. */
String logFile = "log2" + File.separator + "baz.log.xml";
try {
this.logFileHandler = new FileHandler(logFile);
}
catch (IOException io_excp) {
System.err.println(this.CLASS_NAME + "." + METHOD_NAME + " -
Couldn't create FileHandler for logger " + this.CLASS_NAME + " using file "
+ logFile + ". Error: " + io_excp); //$NON-NLS-1$ //$NON-NLS-2$
//$NON-NLS-3$ //$NON-NLS-4$
io_excp.printStackTrace();
return;
}
this.logger.addHandler(this.logFileHandler);
/* Set the logging level. */
this.logger.setLevel(Level.FINEST);
}

}
================================


Here is the source code for Alpha:

================================
package com.foo.baz2;

import java.util.logging.Logger;

public class Alpha {

/**
* The name of this class.
*/
private final String CLASS_NAME = getClass().getName();

/**
* The logger that will log error messages in this class.
*/
private Logger logger = null;
public Alpha() {
super();
}
public Alpha(Logger logger) {
super();
this.logger = logger;
}
public void writeText() {
final String METHOD_NAME = "writeText()";
System.out.println(CLASS_NAME);
logger.entering(CLASS_NAME, METHOD_NAME);
} }
================================


Here is the build file, which is named build2.xml:

================================
<?xml version="1.0" ?><project name="Logging" default="buildall"
basedir=".">
<description>Compile and run the Baz program.
</description>

<property name="domain.slashes" value="com\foo" description="The domain name
written as a path."/>
<property name="jdkbin" value="c:\Program Files\Java\jdk1.6.0_18\bin"
description="The path to the Java JDK binaries."/>
<property name="workspace351" value="c:\eclipse\workspace" description="The
path to the Eclipse 3.5.1 workspace."/>
<property name="baz.proj" value="Logging" description="The name of the Baz
project."/>
<property name="baz.pkg" value="${domain.slashes}\baz2" description="The
name of the package containing the Baz code."/>
<property name="compiler.path" value="${jdkbin}\javac" description="The path
of the Java 1.6.0_18 compiler."/>
<property name="baz.src" value="${workspace351}\${baz.proj}\src"
description="The path to the source files for the Baz code."/>
<property name="baz.bin" value="${workspace351}\${baz.proj}\bin"
description="The path to the class files for the Baz code."/>
<property name="baz.output.dir" value="${workspace351}\${baz.proj}\output"
description="The path to the output directory for the Baz project."/>

<!--==================================================================
Establish patternsets.
==================================================================-->
<patternset id="ps.baz" description="The project-specific Baz code.">
<include name="${baz.pkg}\*.*"/>
</patternset>
<!--==================================================================
Compile all of the Java code, both project-specific and common.
==================================================================-->
<target name="compile" description="Compile the Java code.">
<javac srcdir="${baz.src}" destdir="${baz.bin}" compiler="modern"
fork="yes"
verbose="no" debug="on" debuglevel="lines,vars,source"
deprecation="yes"
executable="${compiler.path}" source="1.6"
description="Compile the code in the baz packages.">
<classpath>
<pathelement path="${baz.bin}"/>
</classpath>
<patternset refid="ps.baz"/>
</javac>
</target>

<!--==================================================================
Run the program via Ant.
==================================================================-->
<target name="run" description="Run the program.">
<echo message="Run the program."/>
<java classname="com.foo.baz2.Supremo" fork="false"
description="Execute the Supremo program.">
<classpath>
<pathelement path="${baz.bin}"/>
</classpath>
</java>
</target>

<!--==================================================================
Do the complete build.
==================================================================-->
<target name="buildall" depends="compile,run" description="Tasks that make
up a complete build.">
<echo message="The build has ended successfully."/>
</target>
</project>
================================


Here is the output from the Ant build when it is run in Eclipse using Ant
1.7.1:

================================
Buildfile: C:\eclipse\workspace\Logging\xml\build2.xml
compile:
run:
[echo] Run the program.
[java] com.foo.baz2.Supremo.configureLogger() - Couldn't create
FileHandler for logger com.foo.baz2.Supremo using file log2\baz.log.xml.
Error: java.io.IOException: Couldn't get lock for log2\baz.log.xml
[java] java.io.IOException: Couldn't get lock for log2\baz.log.xml
[java] at java.util.logging.FileHandler.openFiles(Unknown Source)
[java] at java.util.logging.FileHandler.<init>(Unknown Source)
[java] at com.foo.baz2.Supremo.configureLogger(Supremo.java:61)
[java] at com.foo.baz2.Supremo.<init>(Supremo.java:40)
[java] at com.foo.baz2.Supremo.main(Supremo.java:33)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown
Source)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown
Source)
[java] at java.lang.reflect.Method.invoke(Unknown Source)
[java] at
org.apache.tools.ant.taskdefs.ExecuteJava.run(ExecuteJava.java:217)
[java] at
org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:152)
[java] at org.apache.tools.ant.taskdefs.Java.run(Java.java:764)
[java] at
org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:218)
[java] at
org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:132)
[java] at org.apache.tools.ant.taskdefs.Java.execute(Java.java:105)
[java] at
org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown
Source)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown
Source)
[java] at java.lang.reflect.Method.invoke(Unknown Source)
[java] at
org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
[java] at org.apache.tools.ant.Task.perform(Task.java:348)
[java] at org.apache.tools.ant.Target.execute(Target.java:357)
[java] at org.apache.tools.ant.Target.performTasks(Target.java:385)
[java] at
org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)
[java] at
org.apache.tools.ant.Project.executeTarget(Project.java:1306)
[java] at
org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
[java] at
org.eclipse.ant.internal.ui.antsupport.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
[java] at
org.apache.tools.ant.Project.executeTargets(Project.java:1189)
[java] at
org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:423)
[java] at
org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:137)
[java] com.foo.baz2.Alpha
[java] com.foo.baz2.Beta
[java] com.foo.baz2.Gamma
[java] === Supremo ends ===
buildall:
[echo] The build has ended successfully.
BUILD SUCCESSFUL
Total time: 313 milliseconds

================================

Does anyone know why I'm having this problem? Am I doing something foolish
in the way I create the FileHandler? Or is this what it appears to be, a
weird Ant/Java compatibility issue?

I'm using jdk 1.60.18.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top