0

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

Iceman
  • 321
  • 1
  • 6
  • 21
  • Either use maven to manage dependencies, or explicitly add all the dependencies in the docx4j distribution http://www.docx4java.org/docx4j/docx4j-3.0.1-community.zip or from http://www.docx4java.org/docx4j/docx4j-3_0/dependencies/ – JasonPlutext Feb 26 '14 at 01:46
  • I used maven to manage the dependencies and know it is working. Thank you very much! – Iceman Feb 26 '14 at 12:59

2 Answers2

0

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.

Alan
  • 822
  • 1
  • 16
  • 39
0

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
Ozan
  • 4,345
  • 2
  • 23
  • 35
  • I also tried your solution, but this didn't work. I use maven for the dependencies now. Thanks for your help! – Iceman Feb 26 '14 at 13:00
  • I also used your log4j properties for get rid of the log4j warning. Thanks! – Iceman Feb 26 '14 at 13:44