我有一个maven项目,我正在使用slf4j.log4j。当我在IDE中运行我的项目时,它运行正常,并在控制台上显示日志。
但是,当我将项目打包为jar并运行它时,我得到了
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.hl7.common.StartListeners.<clinit>(StartListeners.java:8)
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
这是我的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hl7</groupId>
<artifactId>appointments-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
</properties>
<dependencies>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>runtime</scope>
<version>1.7.5</version>
</dependency>
</dependencies>
</project>
这是src/main/resources
log4j.rootLogger=DEBUG, consoleAppender, fileAppender
log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=[%t] %-5p %c %x - %m%n
log4j.appender.fileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern=[%t] %-5p %c %x - %m%n
log4j.appender.fileAppender.File=HL7.log
以下是我使用记录器的方法 -
package com.hl7.common;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class StartListeners {
private static final Logger logger = LoggerFactory.getLogger(StartListeners.class);
public static void main(String[] args){
ListenersManager.start();
logger.info("Listeners started.....................");
return;
}
如何使用maven构建我的项目,以便依赖项包含在jar中。我已经阅读了很多答案,但是我越读越困惑。请告诉我需要对pom.xml文件做出哪些更改。
答案 0 :(得分:1)
当您将项目打包为jar时,不包括Maven依赖项。从控制台运行程序时,添加所有maven依赖项或构建“大jar”。 http://orphanware.blogspot.de/2010/09/creating-fat-jar-using-maven.html