Page 1 of 1

Problem with JAXB on MagicDraw plugin with Java 17

PostPosted: Tue Feb 04, 2025 8:17 pm
by Michael
I am converting the code from Java 8 to Java 17 in NetBeans and at the same time I am switching to docx4j-community-11.5.2. I have added all the necessary dependencies from docx4j-community (docx4j-core, docx4j-openxml-*, from "dependencies" and "ImportXHTML" folders) to the project and, according to the instructions, I have added all the jar files from the docx4j-JAXB-ReferenceImpl-11.5.2.zip package. When using JAXB elements in the code, I have added the jakarta.xml.bind.[JAXB element] import everywhere.
The project compiles correctly, but after starting, when trying to load a Word document, I have the exception "Couldn't get [Content_Types].xml from ZipFile".
When working with Java 8 and docx4j 8.3.X, everything worked fine (docx4j-JAXB-Internal was used).

My Java application is a plugin for MagicDraw 2024. In the code I also use "import org.docx4j.jaxb.Context" - does it matter?

Below is the log from my logger after running the plugin in MagicDraw.

Code: Select all
java.util.concurrent.ExecutionException: java.lang.Exception: org.docx4j.openpackaging.exceptions.Docx4jException: Couldn't get [Content_Types].xml from ZipFile
   at java.base/java.util.concurrent.FutureTask.report(Unknown Source)
   at java.base/java.util.concurrent.FutureTask.get(Unknown Source)
   at java.desktop/javax.swing.SwingWorker.get(Unknown Source)
   ...
Caused by: java.lang.Exception: org.docx4j.openpackaging.exceptions.Docx4jException: Couldn't get [Content_Types].xml from ZipFile
   at DocumentHelper.loadFromTemplate(DocumentHelper.java:304)
   ...
   at java.base/java.lang.Thread.run(Unknown Source)
Caused by: org.docx4j.openpackaging.exceptions.Docx4jException: Couldn't get [Content_Types].xml from ZipFile
   at org.docx4j.openpackaging.io3.Load3.get(Load3.java:148)
   at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:572)
   at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:421)
   at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:298)
   at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:276)
   at org.docx4j.openpackaging.packages.WordprocessingMLPackage.load(WordprocessingMLPackage.java:170)
   ...
Caused by: org.docx4j.openpackaging.exceptions.InvalidFormatException: Bad [Content_Types].xml
   at org.docx4j.openpackaging.contenttype.ContentTypeManager.parseContentTypesFile(ContentTypeManager.java:891)
   at org.docx4j.openpackaging.io3.Load3.get(Load3.java:146)
   ... 18 more
Caused by java.lang.NullPointerException: Cannot invoke "jakarta.xml.bind.JAXBContext.createUnmarshaller()" because "org.docx4j.jaxb.Context.jcContentTypes" is null
   at org.docx4j.openpackaging.contenttype.ContentTypeManager.parseContentTypesFile(ContentTypeManager.java:858)
   ... 19 more

Re: JAXB on Java 17 problem - Couldn't get [Content_Types].x

PostPosted: Wed Feb 05, 2025 9:45 pm
by jason
Hi Michael

I tested using docx4j-JAXB-ReferenceImpl-11.5.2.zip and docx4j-community-11.5.2.zip on Java 17.

From those 2 zip files, I had the following libraries on my classpath in eclipse (not modulepath in this quick test):

Code: Select all
    27050 angus-activation-2.0.2.jar
   445288 antlr-2.7.7.jar
   173220 antlr-runtime-3.5.3.jar
   230830 checker-qual-3.42.0.jar
   372608 commons-codec-1.17.0.jar
  1087319 commons-compress-1.27.1.jar
   508826 commons-io-2.16.1.jar
   657952 commons-lang3-3.14.0.jar
  3650883 docx4j-core-11.5.2.jar
     5888 docx4j-JAXB-ReferenceImpl-11.5.2.jar
  2304767 docx4j-openxml-objects-11.5.2.jar
   369922 docx4j-openxml-objects-pml-11.5.2.jar
   600725 docx4j-openxml-objects-sml-11.5.2.jar
    13161 error_prone_annotations-2.3.3.jar
  1567969 fontbox-2.0.30.jar
    25788 istack-commons-runtime-4.1.2.jar
    66514 jakarta.activation-api-2.1.3.jar
   131033 jakarta.xml.bind-api-4.0.2.jar
   138682 jaxb-core-4.0.5.jar
   920213 jaxb-runtime-4.0.5.jar
   290349 jaxb-svg11-11.4.0.jar
    18429 jcl-over-slf4j-2.0.13.jar
    98376 mbassador-1.3.2.jar
   179721 qdox-1.12.jar
    68605 slf4j-api-2.0.13.jar
   148627 stringtemplate-3.2.1.jar
    73266 txw2-4.0.5.jar
    87800 wmf2svg-0.9.11.jar
  1829796 xalan-interpretive-11.0.0.jar
   290138 xalan-serializer-11.0.0.jar
   678189 xmlgraphics-commons-2.9.jar


Running the OpenMainDocumentAndTraverse sample worked fine.

Looking at your stack trace, org.docx4j.jaxb.Context.jcContentTypes should be instantiated when org.docx4j.jaxb.Context is initialised, and the org.docx4j.openpackaging.contenttype package is part of docx4j-core, so it won't be missing.

In answer to your question, importing org.docx4j.jaxb.Context is fine.

I didn't add the ImportXHTML jars for this test.

Can you reproduce your issue with a simple test project (ie outside of MagicDraw)?

Re: JAXB on Java 17 problem - Couldn't get [Content_Types].x

PostPosted: Wed Feb 05, 2025 9:49 pm
by jason
Can you configure debug level logging for org.docx4j.jaxb.Context?

Re: JAXB on Java 17 problem - Couldn't get [Content_Types].x

PostPosted: Fri Feb 07, 2025 12:07 am
by Michael
Hi Jason,

Thank you for the answer.
I have the same dependencies as yours, but when I wrote a simple test with only loading the document, with the same dependencies and docx4j-ImportXHTML-core-11.5.0.jar, I have no error outside of MagicDraw.
My plugin for MagicDraw, in which the error is described above, is written in such a way that it has its own Classloader and takes local dependencies first. Magic Draw has its own jaxb-core-4.0.3.jar and jaxb-runtime-4.0.3.jar, but could this be a problem?

I have not yet prepared detailed logs for Context.

Re: JAXB on Java 17 problem - Couldn't get [Content_Types].x

PostPosted: Tue Feb 11, 2025 10:34 pm
by Michael
I was unable to configure the debug log level for org.docx4j.jaxb.Context correctly in the MagicDraw plugin. However, by trial and error I managed to make the Word document load correctly in the MagicDraw plugin - removing the jakarta.xml.bind-api-4.0.2.jar dependency from my plugin helped!

I don't know why this helped, but I can't even have the jakarta.xml.bind-api-4.0.0.jar dependency that MagicDraw uses. Despite having my own Classloader, it seems to me that JAXB-related files can mix with those from MagicDraw.

Magic Draw uses:
Code: Select all
jaxb-api-2.2.11.jar
jaxb-core-4.0.3.jar
jaxb-runtime-4.0.3.jar
jakarta.xml.bind-api-4.0.0.jar
jakarta.activation-api-2.1.2.jar

Re: Problem with JAXB on MagicDraw plugin with Java 17

PostPosted: Wed Feb 12, 2025 8:46 pm
by Michael
For clarification I would like to add that I had to remove the jakarta.xml.bind-api-4.0.0.jar dependency not from the project libraries folder in NetBeans, but from the dependencies used by my plugin in MagicDraw.