Question about Eclipse and classpath in startup configuration

I just started using Log4J for the first time. I created the log4j.properties file and placed it in the project folder in Eclipse. I also created a launch configuration for my application (this is just the default launch configuration, no additional options).

Now I try to start the application, and I get an error when log4j cannot initialize itself (read the properties file). I know that the properties file must be in the class path, so itโ€™s obvious that Run Run does not set the class path correctly.

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

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

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

Why is this so? Why can't log4j find the properties file unless I manually add the project folder? Isn't this the default classpath folder? (The above conclusion suggests that this is so.)

+4
source share
3 answers

No, the project folder itself is not in the default class path - the project output folders (usually one subfolder named bin or classes).

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

In general, you will not need to get confused with class paths for startup configurations - in most cases it is more advisable to change (or, as in your case, correctly use) the build path in the project properties.

+2
source

the project folder is not added to the classpath by default. By default, the class path contains only the class folder.

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

+1
source

You have two options:

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

The reason it is not working right now is because the file is not in the classpath of the project. You can see the class path from Project> Properties> Java Build Path

0
source

Source: https://habr.com/ru/post/1286283/


All Articles