Page 1 of 1

DocX to PDF Using Java 6

PostPosted: Mon Dec 15, 2008 10:55 pm
by ryan_hire
I created a project in eclipse and created a lib directory with all of the jar files from the expanded tar file in it.
I need to convert a set of docx files to PDF's. So I executed the following piece of code I found on one of the forum posts:
Code: Select all
        File dir = new File( "c:/mydocxfiles");
        for ( File f : dir.listFiles() ) {
            if ( ! f.getAbsolutePath().endsWith( "docx" ) )
                continue;
           
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(f);
           
            String out= f.getAbsolutePath() + ".pdf";
           
            java.io.File temp = new File(out);
            OutputStream os = new java.io.FileOutputStream(temp);
            wordMLPackage.pdf(os);
            os.close();
        }       

It loads up the WordprocessingMLPackage just fine, but when it tries to output it as a PDF it throws:

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/xml/bind/marshaller/NamespacePrefixMapper
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)

In one of the previous forum posts there is a reference to this class by a person who is using Java 5, but I am compiling and executing under Java 6. I do see that NamespacePrefixMapper is part of the docx4j.jar file as well as coming with the Java 6 libraries. So I ordered the jar files in the eclipse "Order and Export" tab to have docx4j.jar to be at the top and I still see the exception. Any help would be appreciated.

Also, where is the best place to see the source for the examples? To just browse the source? or is there a wiki page with instructions?

Re: DocX to PDF Using Java 6

PostPosted: Tue Dec 16, 2008 5:37 am
by codified44

Re: DocX to PDF Using Java 6

PostPosted: Tue Dec 16, 2008 5:40 pm
by ryan_hire
Thanks for the links to the samples. Unfortunately I am still running into the ClassNotFound exception.
I've tried re-ordering the class path and even breaking out to the command line and executing the program with a hand written classpath. I get the same error.

Code: Select all
   at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
   at org.docx4j.openpackaging.parts.JaxbXmlPart.marshal(JaxbXmlPart.java:111)
   at org.docx4j.convert.out.xmlPackage.XmlPackage.saveRawXmlPart(XmlPackage.java:171)
   at org.docx4j.convert.out.xmlPackage.XmlPackage.get(XmlPackage.java:105)
   at org.docx4j.convert.out.html.HtmlExporter.html(HtmlExporter.java:91)
   at org.docx4j.convert.out.html.HtmlExporter.html(HtmlExporter.java:72)
   at org.docx4j.openpackaging.packages.WordprocessingMLPackage.pdf(WordprocessingMLPackage.java:306)
   at src.Convert.main(Convert.java:24)


It appears that it is using / loading the JaxbXML implementation from the docx4j jar/package.
If it helps I am running this on Eclipse on Windows, with JDK 1.6_10.

Re: DocX to PDF Using Java 6

PostPosted: Tue Dec 16, 2008 10:26 pm
by jason
In Eclipse, Run > Open Run Dialog, then for your application, on the "Arguments" tab, add a VM argument similar to the following:

-Djava.endorsed.dirs=/usr/lib/jvm/java-6-sun/jre/lib/endorsed

In that dir I have:

jaxb-api-2.1.jar jaxb-impl-2.1.7.jar jaxb-xjc-2.1.7.jar

You might also find you need to increase memory, with an -Xmx VM argument.

cheers

Jason

Re: DocX to PDF Using Java 6

PostPosted: Wed Dec 17, 2008 6:07 pm
by ryan_hire
Thanks for the reply Jason, but unfortunately that did not help. I did try a different 1.6 JDK instead of version 10 and it also did not work.

To eliminate eclipse from the picture I also tried to run the class with a classpath that I setup and I get the same error.

I did try adding the Accessibility rule to the eclipse project as referenced by the developer notes and that did not help. In looking at the developer notes I see several mentions of "If you are using 1.6 then you don't need to download JAXB" but I am beginning to think that those may not be accurate. Here is the link I found on running with JAXB and 1.6 ( http://dev.plutext.org/trac/docx4j/wiki/JAXB )

I have a couple of questions:
Is anyone successfully working with JDK 1.6 10 and running the project?
Should I download and try to build the entire project and does anyone think that will help at all?
Does anyone have any ideas on why it cannot find a class that is in the default JDK classpath?

This *feels* like one of those "It's the classpath silly!" issues, it is just that the variations that I have tried have not worked.

Here is the error that I am still receiving
Code: Select all
Caused by: java.lang.ClassNotFoundException: com.sun.xml.bind.marshaller.NamespacePrefixMapper
   at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:306)


Any help would be greatly appreciated.

Re: DocX to PDF Using Java 6

PostPosted: Wed Dec 17, 2008 8:27 pm
by ryan_hire
Jason,

If you are using Java 6, why do you need to have the JAXB jars installed or used at all?
I can try to download the jars and see if that helps.

Re: DocX to PDF Using Java 6

PostPosted: Wed Dec 17, 2008 10:46 pm
by jason
Hi Ryan

I'm using Java 6 in Eclipse (usually on Linux, but sometimes Windows).

If I take away -Djava.endorsed.dirs=/usr/lib/jvm/java-6-sun/jre/lib/endorsed, then I get a JAXB error.

Trying this now, it is giving me:
Code: Select all
Exception in thread "main" java.lang.LinkageError: JAXB 2.0 API is being loaded from the bootstrap classloader, but this RI (from jar:file:/home/dev/.m2/repository/com/sun/xml/bind/jaxb-impl/2.1.3/jaxb-impl-2.1.3.jar!/com/sun/xml/bind/v2/model/impl/ModelBuilder.class) needs 2.1 API. Use the endorsed directory mechanism to place jaxb-api.jar in the bootstrap classloader. (See http://java.sun.com/j2se/1.5.0/docs/guide/standards/)


That's because JAXB 2.1.3 Impl is in maven's pom.xml - at least in the code I am running. The error is telling me to use -Djava.endorsed.dirs to make the relevant JAXB API available.

If I comment out that entry in the pom, then I get something similar to you:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
com.sun.xml.bind cannot be resolved to a type

at org.docx4j.jaxb.NamespacePrefixMapper.<init>(NamespacePrefixMapper.java:23)


In short, I'm sure that putting JAXB in your endorsed dir will solve your problem.

The reason you need to do this, is that the JAXB built into Java 6 doesn't have com.sun.xml.bind.marshaller.NamespacePrefixMapper. It has an 'internal' one instead.

So rather than use the internal class (and to make things a bit easier for Java 5 users), we've opted to use the JAXB distribution. If you make a single change to the docx4j source code to use the internal one, you can use the JAXB in Java 6.

cheers .. Jason