关于Eclipse和类路径在运行配置中的问题 [英] A question about Eclipse and classpath in a Run configuration
问题描述
现在,我尝试运行应用程序,并收到一条错误消息关于log4j无法初始化自己(读取属性文件)。我知道属性文件必须在类路径中,所以显然运行配置没有正确设置类路径。
如果我去我的运行配置的Classpath选项卡,我有以下内容:
- 引导条目
- 用户条目
- MyApplication(默认类路径)
- log4j-1.2.15.jar - C:\Workspace\MyApplication\lib
但是,如果我手动添加项目的文件夹(我单击添加文件夹,高级,MyApplication) ,log4j将能够自己初始化。
为什么会这样?为什么log4j找不到属性文件,除非我手动添加项目的文件夹?默认情况下,classpath中不存在此文件夹? (上面的输出会建议它是。)
不,默认情况下,项目文件夹本身不在类路径中 - 项目的输出文件夹(通常是一个名为bin或类的子文件夹)。
如果将您的log4j.properties放入项目的源文件夹而不是其根文件夹,那么一切都应该有效(源文件夹中的非源文件被复制到输出文件夹自动)。
一般来说,你不必弄乱运行配置的类路径 - 在大多数情况下,更合适的是(或者像你的情况) ,正确使用)项目属性中的构建路径。
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.)
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.
这篇关于关于Eclipse和类路径在运行配置中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!