我想在我的java项目中使用docx4j来读取docx文档。我正在使用Eclipse。我下载了docx4j-jar文件并将其包含在构建路径中。运行我的测试代码时,我收到以下错误消息: 线程“main”中的异常java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory
我将log4j-1.2.17.jar,slf4j-api-1.7.5.jar和slf4j-log4j12-1.7.5.jar添加到我的项目构建路径中。但是我收到此错误消息
log4j:WARN No appenders could be found for logger (org.docx4j.jaxb.Context).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/io/IOUtils
at org.docx4j.openpackaging.io3.Load3.get(Load3.java:138)
at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:353)
at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:293)
at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:243)
at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:226)
at org.docx4j.openpackaging.packages.WordprocessingMLPackage.load(WordprocessingMLPackage.java:162)
at org.docx4j.Docx4J.load(Docx4J.java:176)
at Doc4JReadDOCX.main(Doc4JReadDOCX.java:11)
我还尝试添加commons-logging-1.1.3.jar,但这不会改变任何内容。
在项目中使用docx4j需要做什么?我需要添加哪些.jar文件? 谢谢你的帮助! 1ceman
答案 0 :(得分:0)
您的错误消息是试图告诉您需要初始化Log4j。通常,您需要告诉它将错误消息打印到控制台或文件。所以你需要设置一个属性文件:
# Root logger option
log4j.rootLogger=DEBUG, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
然后,创建一个servlet,通过读取属性文件来初始化log4j。像这样:
import org.apache.log4j.PropertyConfigurator;
import javax.servlet.http.*;
import com.mysite.*;
public class Log4jInit extends HttpServlet
{
public void init()
{
try{
String prefix = getServletContext().getRealPath("/");
String file = getInitParameter("log4j-init-file");
if(file != null)
{
System.out.println("Log4jInit.init - prefix + file: " + prefix + file);
PropertyConfigurator.configure(prefix + file);
}
}
catch(Exception e){e.printStackTrace();}
}
public void doGet(HttpServletRequest req, HttpServletResponse res){}
}
在您的web.xml文件中,您需要输入以下内容:
<servlet>
<servlet-name>log4j-init</servlet-name>
<servlet-class>com.mysite.Log4jInit</servlet-class>
<init-param>
<param-name>log4j-init-file</param-name>
<param-value>WEB-INF/properties/log4j_CONSOLE.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>log4j-init</servlet-name>
<url-pattern>/log4j-init/*</url-pattern>
</servlet-mapping>
希望这有帮助。
答案 1 :(得分:0)
1)您缺少类路径中的org.apache.commons.io库。 docx4j需要它。
2)抑制log4j警告:
将文件log4j.properties
放在根源目录中(通常为src
)。在其中配置log4j。示例文件如下:
log4j.rootLogger=ERROR, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c %x - %m%n