Community
Participate
Working Groups
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.
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); } }
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.
Thanks..I can reproduce the problem...looking into it.
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.
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.
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.
*** Bug 40183 has been marked as a duplicate of this bug. ***