Bug 30353 - log4j.properties file not found
Summary: log4j.properties file not found
Status: RESOLVED WONTFIX
Alias: None
Product: Platform
Classification: Eclipse Project
Component: Ant (show other bugs)
Version: 2.1   Edit
Hardware: PC Windows XP
: P2 normal (vote)
Target Milestone: 2.1 M5   Edit
Assignee: Darin Swanson CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 40183 (view as bug list)
Depends on:
Blocks:
 
Reported: 2003-01-28 02:47 EST by Peter John Golda CLA
Modified: 2003-08-05 14:11 EDT (History)
1 user (show)

See Also:


Attachments
Sample Program external tool configuration for launching Ant in separate VM (1.20 KB, text/plain)
2003-02-05 10:48 EST, Darin Swanson CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Peter John Golda CLA 2003-01-28 02:47:45 EST
I have a Java project. The Java project uses log4j. I have a log4j.properties 
file in the same directory as the Ant build.xml file. I have (in a separate 
source directory, but the same package) a set of JUnit tests.

I go to my command prompt. I go to the directory with the build.xml file, and 
say

ant utest

and all my tests run and pass. Now, I go to Eclipse. I can run ALL my targets 
(build, build with unit tests, create dist pack, create jar, export jar to 
other projects, javadoc, etc, etc)... except one. The one that does not run 
is "utest". The error is:

utest:
log4j:ERROR Could not read configuration file [log4j.properties].
java.io.FileNotFoundException: log4j.properties (The system cannot find
the file specified)

1. I have set the working directory on the Run Ant dialog to the correct 
place - no difference.
2. I have searched your bugs for log4j - no results.

Thus... at a guess it seems like Eclipse is losing my working directory, and 
hence log4j.properties is not found.

I include the utest target:

<project default="ubuild" basedir=".">

<target name="utest" depends="ubuild">
	<junit>
		<classpath refid="project.classpath"/>
		<test name="${test}"/>
		<formatter type="brief" usefile="no"/>
	</junit>
</target>

</project>

where ubuild works just fine, and the test name is AllTests. This is standard 
stuff and works on the command line in Linux and Windows, and in Eclipse 2.0 
20020920 - yes, the one with the nice Ant interface :-) which is still being 
used by the rest of the team. Changing the project basedir in the Ant file 
does not have any effect. The ant version I installed manually is the same as 
the ant version that comes with Eclipse 2.1 20021218.

Easiest way for you to test may be to get some old code that also has unit 
tests, slap in an initialisation line for log4j

private static Category cat = Category.getInstance( MyClass.class );

slap a log4j.properties file in the same place the the ant build file, for 
example:

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out  
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p [%t] %-15c{1}: %m; line %
L%n
log4j.rootCategory=FATAL,console

add the utest target to your build file, and then try it with an older and a 
new version of Eclipse.
Comment 1 Darin Swanson CLA 2003-01-30 11:08:28 EST
I know nothing about log4j but I can't reproduce the problem (I don't think). 
It does not seem to ever be going to find the log4j.properties file?
This is the output I get:

Buildfile: C:\Darins\workspaces\Bug30353\buildFiles\log4j.xml

utest:
       [junit] Testsuite: test.Log4jTest
       [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.01 sec
       [junit] 
BUILD SUCCESSFUL
Total time: 291 milliseconds

I know that it is not accessing the log4j.properties file as if I remove it the 
tests still pass?

Test class:
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.log4j.Category;

public class Log4jTest extends TestCase {

	
	private static Category cat = Category.getInstance(Log4jTest.class);

	public static void main(String[] args) {
		junit.textui.TestRunner.run(suite());
	}

	public static Test suite() {
		return new TestSuite(Log4jTest.class);
	}

	public void testNoProperties() {
		assertNotNull(this);
	}
}
Comment 2 Peter John Golda CLA 2003-01-31 02:45:11 EST
You are right, I left out a line of code! Humble apologies. Please add the 
following import:

import org.apache.log4j.PropertyConfigurator;

and the following code somewhere - for example, in your main():

PropertyConfigurator.configure( "log4j.properties" );

Eclipse will then fail to find the file. YES - you can hard code the path to 
this file and then everything works, but that is not the point. This path and 
file name should always be relative to the working directory.

Once again, apologies for the omission.
Comment 3 Darin Swanson CLA 2003-01-31 09:18:41 EST
Thanks..I can reproduce the problem...looking into it.
Comment 4 Darin Swanson CLA 2003-01-31 11:49:22 EST
The whole problem is that it is looking for the file relative to the working 
directory.
Since the Eclipse Ant integration runs in the same VM, the working directory is 
set to the directory from which Eclipse was started from. The obviously causes 
problems when using classes like FileInputStream etc for dealing with finding 
and opening resources. Setting the basedir for Ant is not the same.

We currently have no nice workaround for this. In bug 24129, I will attach 
launch config(s) that can be used to launch Ant from eclipse in a separate VM. 
We are working on better variable support so that these can be as generic as 
possible and be easily shared within a team.

As well have you tried using the JUnit support within Eclipse?  You can set the 
working directory (as it is launched in a separate VM) and these could be 
shared with the project. You test case works fine with this support.

Sorry for the trouble this has caused you.
Comment 5 Darin Swanson CLA 2003-02-03 11:46:30 EST
When running in the same VM as Eclipse this will have to be a README item.
Marking as WONTFIX as there is no way to handle this better when running in the 
same VM.
See bug 24129.
Comment 6 Darin Swanson CLA 2003-02-05 10:48:21 EST
Created attachment 3298 [details]
Sample Program external tool configuration for launching Ant in separate VM

This .launch file should be placed in your "log4j" project. As well you will
need to create a "Jakarta" project that contains the necessary JARs as
needed(ant, optional, junit, log4j, xerces, xml-apis). This project can be
external to the workspace and shared by a team without having to put all the
JARs in a repository.
Comment 7 Darin Swanson CLA 2003-08-05 14:11:57 EDT
*** Bug 40183 has been marked as a duplicate of this bug. ***