eclipse 无法导入 org.slf4j.Logger- 如何将 jar 添加到类路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32221203/
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
Fail to import org.slf4j.Logger- how to add the jar to classpath?
提问by user3735871
I am learning to use Maven and Log4J. In the POM file I've added the below dependencies:
我正在学习使用 Maven 和 Log4J。在 POM 文件中,我添加了以下依赖项:
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
When I tried to import import org.slf4j.Logger
in my main class, Eclipse says "The import org.slf4j cannot be resolved". I searched a while for an solution, and it seems that I need to download the distribution file here http://www.slf4j.org/download.htmland "add the jar to the classpath"? I am confused: wouldn't Maven download that automatically? How can I "add the jar to the classpath"? I am a real beginner here. Many thanks for your help!
当我尝试import org.slf4j.Logger
在主类中导入时,Eclipse 显示“无法解析导入 org.slf4j”。我搜索了一段时间寻找解决方案,似乎我需要在这里下载分发文件http://www.slf4j.org/download.html并“将 jar 添加到类路径”?我很困惑:Maven 不会自动下载吗?如何“将 jar 添加到类路径”?我是这里的真正初学者。非常感谢您的帮助!
回答by user1933888
You said you are new to Maven and have added the dependency in POM. Now I am assuming that, you are assuming that, just because you added something in pom.xml, the dependent jars will be available for build.
您说您是 Maven 的新手,并在 POM 中添加了依赖项。现在我假设,你假设,仅仅因为你在pom.xml 中添加了一些东西,依赖的 jars 将可用于构建。
Maven does not work that way. It points to a common repository from where you have to download the dependent jars (which happens when you do an mvn install)
Maven 不会那样工作。它指向一个公共存储库,您必须从中下载依赖的 jars(当您执行mvn install时会发生这种情况)
After you mvn install works fine, you may use this src to setup in your eclipse IDE either by using mvn eclipse:eclipseor using an eclipse maven plugin.
在 mvn install 工作正常后,您可以使用此 src 在您的 Eclipse IDE 中进行设置,方法是使用mvn eclipse:eclipse或使用 eclipse maven 插件。
So, after you added the dependency, run following command from where your project is located
因此,在添加依赖项后,从项目所在的位置运行以下命令
mvn clean install;
Your jars should be downloaded from maven to your local directory something like:
你的 jars 应该从 maven 下载到你的本地目录,比如:
C:\Users\user.m2\repository
C:\Users\user.m2\repository
回答by Laszlo Lugosi
As I had run to very similar issue, I write what my problem was:
由于我遇到了非常相似的问题,所以我写了我的问题是什么:
I copied a wrong maven dependency from the internet (example). It was only in test scope.. Oh Dear..
我从互联网上复制了一个错误的 maven 依赖项(示例)。它只在测试范围内.. 哦,亲爱的..
<scope>test</scope>
So after remove scope test section started to work as expected. :)
因此,在删除范围测试部分后开始按预期工作。:)
回答by szefuf
You also need dependency to slf4j-api
你还需要依赖 slf4j-api
Also, if you use eclipse, it might be the case - try to update project.
此外,如果您使用 eclipse,则可能是这种情况 - 尝试更新项目。