Page 1 of 1

Null Class Loader

PostPosted: Thu May 05, 2011 1:18 am
by arimmer
We're using docx4j to produce Word 2007 output from our application, but are having a problem with null class loaders.

in org.docx4j.jaxb.Context there is the following code:

Code: Select all
try {   
         
         // JBOSS might use a different class loader to load JAXBContext, which causes problems,
         // so explicitly specify our class loader.
         Context tmp = new Context();
         java.lang.ClassLoader classLoader = tmp.getClass().getClassLoader();
         //log.info("\n\nClassloader: " + classLoader.toString() );         
         
         log.info("loading Context jc");      


However, when our application is running standalone (running from command line) the classLoader variable is null.

The command line looks like:
Code: Select all
..\jre6\bin\java -Dlog4j.configuration=.\conf\log4j.xml -Djava.endorsed.dirs=".\endorsed" -Xms1024m -Xmx1024m -server -jar .<myJarFile>


Is there some configuration we should be adding to ensure we get a value for classLoader?

Running the application as an applet through our IDE picks up a Tomcat class loader successfully and docx4j then works fine, but we need it to work standalone too.

Re: Null Class Loader

PostPosted: Thu May 05, 2011 2:19 am
by jason
You could try removing the explicit classloader (as per below .. if it works, you'll need to do it in a few other places as well, eg jcXslFo)

Code: Select all
### Eclipse Workspace Patch 1.0
#P docx4j-2.6.0
Index: src/main/java/org/docx4j/jaxb/Context.java
===================================================================
--- src/main/java/org/docx4j/jaxb/Context.java   (revision 1451)
+++ src/main/java/org/docx4j/jaxb/Context.java   (working copy)
@@ -63,31 +63,25 @@
        }
      
      try {   
-         
-         // JBOSS might use a different class loader to load JAXBContext, which causes problems,
-         // so explicitly specify our class loader.
-         Context tmp = new Context();
-         java.lang.ClassLoader classLoader = tmp.getClass().getClassLoader();
-         //log.info("\n\nClassloader: " + classLoader.toString() );         
-         
+                  
         log.info("loading Context jc");         
         jc = JAXBContext.newInstance("org.docx4j.wml:" +
               "org.docx4j.dml:org.docx4j.dml.chart:org.docx4j.dml.chartDrawing:org.docx4j.dml.compatibility:org.docx4j.dml.diagram:org.docx4j.dml.lockedCanvas:org.docx4j.dml.picture:org.docx4j.dml.wordprocessingDrawing:org.docx4j.dml.spreadsheetdrawing:" +
               "org.docx4j.vml:org.docx4j.vml.officedrawing:" +
               "org.opendope.xpaths:org.opendope.conditions:org.opendope.questions:org.opendope.components:" +
-               "org.docx4j.math",classLoader );
+               "org.docx4j.math");
         log.info("loaded " + jc.getClass().getName() + " .. loading others ..");
         
         jcThemePart = jc; //JAXBContext.newInstance("org.docx4j.dml",classLoader );
-         jcDocPropsCore = JAXBContext.newInstance("org.docx4j.docProps.core:org.docx4j.docProps.core.dc.elements:org.docx4j.docProps.core.dc.terms",classLoader );
-         jcDocPropsCustom = JAXBContext.newInstance("org.docx4j.docProps.custom",classLoader );
-         jcDocPropsExtended = JAXBContext.newInstance("org.docx4j.docProps.extended",classLoader );
-         jcXmlPackage = JAXBContext.newInstance("org.docx4j.xmlPackage",classLoader );
-         jcRelationships = JAXBContext.newInstance("org.docx4j.relationships",classLoader );
-         jcCustomXmlProperties = JAXBContext.newInstance("org.docx4j.customXmlProperties",classLoader );
-         jcContentTypes = JAXBContext.newInstance("org.docx4j.openpackaging.contenttype",classLoader );
+         jcDocPropsCore = JAXBContext.newInstance("org.docx4j.docProps.core:org.docx4j.docProps.core.dc.elements:org.docx4j.docProps.core.dc.terms" );
+         jcDocPropsCustom = JAXBContext.newInstance("org.docx4j.docProps.custom" );
+         jcDocPropsExtended = JAXBContext.newInstance("org.docx4j.docProps.extended");
+         jcXmlPackage = JAXBContext.newInstance("org.docx4j.xmlPackage" );
+         jcRelationships = JAXBContext.newInstance("org.docx4j.relationships" );
+         jcCustomXmlProperties = JAXBContext.newInstance("org.docx4j.customXmlProperties" );
+         jcContentTypes = JAXBContext.newInstance("org.docx4j.openpackaging.contenttype");
         
-         jcSectionModel = JAXBContext.newInstance("org.docx4j.model.structure.jaxb",classLoader );
+         jcSectionModel = JAXBContext.newInstance("org.docx4j.model.structure.jaxb" );
         
         log.info(".. others loaded ..");
         

Re: Null Class Loader

PostPosted: Thu May 05, 2011 4:31 am
by arimmer
Thanks for your help.

Have added a check to see if the classLoader variable is null, if it isn't we use it otherwise we don't try and now it is working fine.