抛出错误通过Maven运行slf4j-api

时间:2014-06-04 12:59:09

标签: java maven

在我的porm.xml中输入slf4j

enter code here
   <dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.7</version>
  <scope>compile</scope>
</dependency>

得分代码:

package test.test1;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * Hello world!
 *
*/
public class App 
{
  public static void main( String[] args )
  {
    //System.out.println( "Hello World!" );
Logger logger = LoggerFactory.getLogger("App.class");
logger.info("Test");

  }
}

通过Maven编译成功,但在抛出Running Exception期间。

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFacto
ry at test.test1.App.main(App.java:14)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 1 more

应手动为slf4j jar文件设置类路径??

1 个答案:

答案 0 :(得分:0)

Maven从本地存储库中获取您在pom.xml中定义的依赖项,以便一切编译良好。但是,当您从脚本或命令行启动应用程序时,需要通过classpath指定找到必要类的位置。

对于日食,有m2e&amp;将maven与IDE集成的m2e-wtp插件,因此您无需逐个手动添加jar。相反,你将拥有一种虚拟的maven库&#34;其中包含对所有依赖项的引用,如下图所示: maven library