简体   繁体   中英

How to use docx4j in my project?

I want to use docx4j in my java project to read docx documents. I am using Eclipse. I downloaded the docx4j-jar file and included it in the build path. When running my test code i get this error message: Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

Than i added log4j-1.2.17.jar, slf4j-api-1.7.5.jar and slf4j-log4j12-1.7.5.jar to my project build path. But than i get this error message

    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)

I also tried to add commons-logging-1.1.3.jar, but this does not change anything.

What do I need to do to use docx4j in my project? What .jar files do i need to add? Thanks for helping! 1ceman

Your error message is trying to tell you that you need to initialize Log4j. Usually you need to tell it to print error messages to the console or to a file. So you need to set up a property file like so:

# 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

Then, create a servlet that initializes log4j by reading in the properties file. Something like this:

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){}
}

In your web.xml file you'll need to put the following:

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

Hope this helps.

1) You are missing the org.apache.commons.io library in your classpath. docx4j needs it.

2) To suppress the log4j warning: Put a file log4j.properties in your root source directory (normally src ). In it you configure log4j. A sample file is like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM