关于运行配置中的 Eclipse 和类路径的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1000546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:48:05  来源:igfitidea点击:

A question about Eclipse and classpath in a Run configuration

eclipseclasspath

提问by Ariod

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).

我刚刚开始第一次使用 Log4J。我创建了一个 log4j.properties 文件并将其放在 Eclipse 中我的项目文件夹中。我还为我的应用程序创建了一个运行配置(它只是一个默认的运行配置,没有额外的选项)。

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.

现在,我尝试运行该应用程序,但收到一条关于 log4j 无法自行初始化(读取属性文件)的错误消息。我知道属性文件必须在类路径中,所以显然运行配置没有正确设置类路径。

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
  • 引导条目
  • 用户条目
    • MyApplication(默认类路径)
    • 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.

但是,如果我手动添加我的项目文件夹(我单击添加文件夹、高级、MyApplication),log4j 将能够自行初始化。

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.)

为什么呢?为什么除非我手动添加项目的文件夹,否则 log4j 找不到属性文件?默认情况下,这个文件夹不是在类路径中吗?(上面的输出表明它是。)

采纳答案by Michael Borgwardt

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).

不,默认情况下,项目文件夹本身不在类路径中 - 项目的输出文件夹是(通常是一个名为 bin 或 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).

如果您将 log4j.properties 放入项目的源文件夹而不是其根文件夹,则一切正常(源文件夹中的非源文件会自动复制到输出文件夹)。

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.

通常,您不必弄乱运行配置的类路径 - 在大多数情况下,更改(或者,在您的情况下,正确使用)项目属性中的构建路径更合适。

回答by Rahul

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.

您必须手动将任何其他文件夹添加到类路径。

回答by David Rabinowitz

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
  • 将 log4j.properties 文件放在 src 文件夹下
  • 创建一个额外的源文件夹,通常命名资源或资源,并将 log4j.properties 文件放在那里

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

它现在不起作用的原因是该文件不在项目的类路径中。您可以从 Project > Properties > Java Build Path 中看到类路径