views:

995

answers:

3

I've just started using Log4J for the first time. I created a log4j.properties file and put it in my project's folder in Eclipse. I also created a Run configuration for my application (it's just a default Run configuration, no extra options).

Now, I try running the application and I get an error message about log4j not being able to initialize itself (read the properties file). I know that the properties file must be in the classpath, so obviously the Run configuration is not setting the classpath properly.

If I go to the Classpath tab of my Run configuration, I have the following:

  • Bootstrap Entries
  • User Entries
    • MyApplication (default classpath)
    • log4j-1.2.15.jar - C:\Workspace\MyApplication\lib

However, if I add my project's folder manually (I click Add Folders, Advanced, MyApplication), log4j will be able to initialize itself.

Why is that so? Why log4j cannot find the properties file unless I add the project's folder manually? Isn't this folder in the classpath by default? (The above output would suggest that it is.)

+1  A: 

the project folder is not added to the classpath by default. The classpath only contains the classes folder by default.

You will have to manually add any other folder to the classpath.

Rahul
A: 

You have two options:

  • put the log4j.properties file under your src folder
  • Create an additional source folder, usually names resources or res, and put the log4j.properties file there

The reason it's not working now is that the file is not in the classpath of the project. You can see the classpath from Project > Properties > Java Build Path

David Rabinowitz
A: 

No, the project folder itself is not in the classpath by default - the project's output folders are (usually one subfolder named bin or classes).

If you put your log4j.properties into your project's source folder instead of its root folder, then everything should work (non-source files in the source folder are copied to the output folder automatically).

In general, youshould not have to mess with the classpath of run configurations - in most cases it's more appropriate to change (or, as in your case, correctly use) the build path in the project's properties.

Michael Borgwardt