Expand my Community achievements bar.

SOLVED

java.lang.NoClassDefFoundError in OSGi even when dependencies are added in POM CQ5

Avatar

Level 4

I am trying to convert an .docx file to html using apache POI. Now the case is when I deploy my bundle to OSGi felix in CQ5 I am getting the exception as  :

java.lang.NoClassDefFoundError: org/openxmlformats/schemas/wordprocessingml/x2006/main/impl/CTSectPrImpl$1HeaderReferenceList at org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTSectPrImpl.getHeaderReferenceList(Unknown Source) at org.apache.poi.xwpf.converter.core.MasterPageManager.visitHeadersFooters(MasterPageManager.java:200) at org.apache.poi.xwpf.converter.core.MasterPageManager.addSection(MasterPageManager.java:178) at org.apache.poi.xwpf.converter.core.MasterPageManager.compute(MasterPageManager.java:125) at org.apache.poi.xwpf.converter.core.MasterPageManager.initialize(MasterPageManager.java:95) at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitBodyElements(XWPFDocumentVisitor.java:226) at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.start(XWPFDocumentVisitor.java:193) at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.convert(XHTMLConverter.java:68) at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.doConvert(XHTMLConverter.java:58) at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.doConvert(XHTMLConverter.java:38) at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:45) at com.maximintegrated.wcm.handlers.SoftwarePartDownloadHandler.wordFileFrmURLToHTML(SoftwarePartDownloadHandler.java:112) at com.maximintegrated.wcm.handlers.SoftwarePartDownloadHandler.activate(SoftwarePartDownloadHandler.java:56)
  • I tried creating the simple java project and that works like a charm when I added all the required jars in configure build path -> external jars .
  • I created a maven project also and added all the dependencies in its POM and this also worked fine. POM file dependencies are pom.xml.
  • But when I create a maven project for AEM6 with the same dependencies and deploy to OSGi felix, there is no compile time error but at runtime I get the above exception. have tried it in various CQ instances. Still no luck.
    Also I tried building the ooxml-schemas-1.1.jar as bundle and installing it separately in felix OSGi because thats the jar where that class in coming from, but that also gives the same error.
1 Accepted Solution

Avatar

Correct answer by
Employee

Hi,

What you need to do is properly embed the required jars in your bundle.  You must keep in mind that it will not only be the jars you directly depend on, but also that jar's transitive dependencies.

in your pom you will need to embed like this in the configuration instructions for the maven-bundle-plugin:

<configuration><instructions><Bundle-Category>granite,workflow</Bundle-Category><Embed-Dependency>*;scope=compile</Embed-Dependency>        <Embed-Dependency>*;scope=compile</Embed-Dependency>        <Embed-Transitive>true</Embed-Transitive>
</instructions></configuration>

I use the scope=compile trick so that all of the compile scoped dependencies are included. 

If the dependencies you are including are large and have lots of transitive dependencies, you may need to exclude some parts, whcih can be done by adding this to the instructions: (sometimes needed for dependencies which are not needed)

<Import-Package>!org.apache.tools.ant.*,!org.objectweb.asm.*,*</Import-Package>

This can be an error prone process, and might require a few iterations to get it working.  Good luck.

The maven bundle plugin is documented here:  http://felix.apache.org/documentation/subprojects/apache-felix-maven-bundle-plugin-bnd.html

Will

View solution in original post

10 Replies

Avatar

Level 2

after building the bundle in target,check whether the class you are executing is in bundle or not...

Avatar

Level 4

Hi Santosh,

Actually my class is already there and from my class only the statement  XHTMLConverter.getInstance().convert(document, out, options); is throwing exception as internally somehow its referring to org/openxmlformats/schemas/wordprocessingml/x2006/main/impl/CTSectPrImpl$1HeaderReferenceList which it is unable to find. So the issue is how do I make OSGi to locate this class form the jar which I have provided in <dependency> of pom.xml.

Thanks 

Avatar

Level 10

AEM will pick up a package that is exposed by any OSGI in the AEM service container. When you deploy to Felix - are you seeing missing packages? 

Avatar

Correct answer by
Employee

Hi,

What you need to do is properly embed the required jars in your bundle.  You must keep in mind that it will not only be the jars you directly depend on, but also that jar's transitive dependencies.

in your pom you will need to embed like this in the configuration instructions for the maven-bundle-plugin:

<configuration><instructions><Bundle-Category>granite,workflow</Bundle-Category><Embed-Dependency>*;scope=compile</Embed-Dependency>        <Embed-Dependency>*;scope=compile</Embed-Dependency>        <Embed-Transitive>true</Embed-Transitive>
</instructions></configuration>

I use the scope=compile trick so that all of the compile scoped dependencies are included. 

If the dependencies you are including are large and have lots of transitive dependencies, you may need to exclude some parts, whcih can be done by adding this to the instructions: (sometimes needed for dependencies which are not needed)

<Import-Package>!org.apache.tools.ant.*,!org.objectweb.asm.*,*</Import-Package>

This can be an error prone process, and might require a few iterations to get it working.  Good luck.

The maven bundle plugin is documented here:  http://felix.apache.org/documentation/subprojects/apache-felix-maven-bundle-plugin-bnd.html

Will

Avatar

Level 4

I am not seeing any missing package and OSGi AEM is unable to locate the class. Because may be I am not directly using that class from my code but its being referred to by some other class internally. 

Avatar

Level 4

Hi Will,

I tried the way you have suggested and changed like below but unfortunately still getting the same error. Earlier I tried with 

<Embed-Dependency>*;scope=compile</scope> as well but because of that lot of "Cannot be resolved" errors in bundle came up due to which I changed it to <Embed-Dependency>ooxml-schemas;scope=compile</scope>. 

Do you see anything else which I can try out.

<plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Bundle-SymbolicName>my-group-id.myproject-bundle</Bundle-SymbolicName> <Bundle-Category>cq5</Bundle-Category> <Embed-Dependency>ooxml-schemas;scope=compile</Embed-Dependency> <Embed-Transitive>true</Embed-Transitive> </instructions> </configuration> </plugin>

Avatar

Level 9

Hi,

Since, Apache POI works fine and able to read doc files in an independent project, then we need to verify two things:

  1. Apache Felix already has POI in their container so don't override it in the OSGI container.
  2. Compare the POI version in Felix console and Make sure, you have used same POI version in your maven dependency.
  3. Make sure you have MS office installed in your environment. FYI:  If you are using Office Libra, etc. Then Apache POI does not work properly. 

---

Jitendra

Avatar

Level 4

Jitendra S.Tomar wrote...

Hi,

Since, Apache POI works fine and able to read doc files in an independent project, then we need to verify two things:

  1. Apache Felix already has POI in their container so don't override it in the OSGI container.
  2. Compare the POI version in Felix console and Make sure, you have used same POI version in your maven dependency.
  3. Make sure you have MS office installed in your environment. FYI:  If you are using Office Libra, etc. Then Apache POI does not work properly. 

---

Jitendra

 

Hi Jitendra,

Thanks a lot for helping me, I have few questions :

1.  I can see that the bundle OSGi bundle wrapper for the Apache POI library (com.adobe.granite.poi) has all the dependencies already embedded in it like scratchpad, xmlbeans, dom4j etc as you pointed out correctly. So should I remove those from the pom.xml which I explicitly added as <dependency> and instead add only one <dependency> as below ?

<dependency> <groupId>com.adobe.granite</groupId> <artifactId>com.adobe.granite.poi</artifactId> <version>1.1.2</version> <scope>provided</scope> </dependency>

2. If I add above dependency Maven is unable to resolve and giving "Missing artifact" error in pom.xml http://pastebin.com/Dv0sP9iW. Where should I get this maven dependency.

3. MS Office 2013 is installed in my machine where CQ5 instance in present.

Thanks,

Pankaj

Avatar

Level 9

Hey Pankaj,

Yes, you should be using only one dependency (which you mentioned in your comment). Regarding missing artifact, there could be two possibilities:

  1. Either repository which you are using does not have POI. So check the repository.
  2. Make sure dependencies are added correctly (parent module & place where we do the configuration in the POM without version)

These links could give more information about it.

http://stackoverflow.com/questions/6111408/maven2-missing-artifact-but-jars-are-in-place

http://stackoverflow.com/questions/18108080/errors-in-pom-xml-with-dependencies-missing-artifact

--

Jitendra

Avatar

Level 4

Hi Jitendra,

Seems like the version 1.1.2 is not present so I added the previous version i.e. 1.1.0 and the project compiled successfully and I even de compiled this jar to check CTSectPrImpl was present at org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTSectPrImpl.

<dependency> <groupId>com.adobe.granite</groupId> <artifactId>com.adobe.granite.poi</artifactId> <version>1.1.0</version> <scope>provided</scope> </dependency> 

But unfortunately even after doing this again the same error came :

09.12.2015 13:38:07.114 *ERROR* [0:0:0:0:0:0:0:1 [1449648469071] GET /content/sladocumentdemo.html HTTP/1.1] com.day.cq.wcm.core.impl.WCMDeveloperModeFilter Error during include of SlingRequestPathInfo: path='/content/sladocumentdemo/jcr:content/par/sla_iframe', selectorString='null', extension='html', suffix='null' java.lang.NoClassDefFoundError: org/openxmlformats/schemas/wordprocessingml/x2006/main/impl/CTSectPrImpl$1HeaderReferenceList at org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTSectPrImpl.getHeaderReferenceList(Unknown Source) at org.apache.poi.xwpf.converter.core.MasterPageManager.visitHeadersFooters(MasterPageManager.java:200) at org.apache.poi.xwpf.converter.core.MasterPageManager.addSection(MasterPageManager.java:178) at org.apache.poi.xwpf.converter.core.MasterPageManager.compute(MasterPageManager.java:125) at org.apache.poi.xwpf.converter.core.MasterPageManager.initialize(MasterPageManager.java:95) at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitBodyElements(XWPFDocumentVisitor.java:226) at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.start(XWPFDocumentVisitor.java:193) at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.convert(XHTMLConverter.java:68) at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.doConvert(XHTMLConverter.java:58) at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.doConvert(XHTMLConverter.java:38) at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:45) at com.testcompany.wcm.handlers.SoftwarePartDownloadHandler.wordFileFrmURLToHTML(SoftwarePartDownloadHandler.java:114) at com.testcompany.wcm.handlers.SoftwarePartDownloadHandler.activate(SoftwarePartDownloadHandler.java:58)

Please help me why again the classdefnotfounderror is coming if the class in there in the referenced jar.

 

Thanks,

Pankaj