RE: OSGi: using Third party .jar results in NoClassDefFoundError's

Jürgen Untch, modified 5 Years ago. New Member Posts: 21 Join Date: 11/14/19 Recent Posts
Hi! I developed an OSGi module (Liferay 7.0 EE, service archetype) which uses jasperreports jar (v6.12.2) to generate PDFs. After Deployment I get a lot  of errors (e.g. java.lang.NoClassDefFoundError: org/apache/commons/collections4/map/ReferenceMap). What do I have to do so that:

a.) my osgi module jar contains the neccessary jar and its dependencies.
b.) no more errors appear in the logs.To get access to the jar, I added it as a dependency in the the POM.

pom.xml
<dependency>
&nbsp; <groupid>net.sf.jasperreports</groupid>
&nbsp; <artifactid>jasperreports</artifactid>
&nbsp; <version>6.12.2</version>
&nbsp; <scope>compile</scope>
</dependency>

bnd.bnd
Bundle-Name: jasper
Bundle-SymbolicName: com.example.service.jasper
Bundle-Version: 1.0.0
Bundle-Activator: com.example.service.activator.JasperReportServiceActivator
Export-Package: com.example.service.jasper.api

Are there any clear instructions what to do in this specific scenario? I only found vague ones.
thumbnail
David H Nebinger, modified 5 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
Since you are using compile scope, jasper is used to compile your module but is not including it or any transitive dependencies into the module. This is what is causing all of your no class def errors, unresolved references, etc.

When building your module, you need to either include the deps in the module (shadow them in or build an uber jar) or they have to be deployed and available in the OSGi container (potential issues here in dealing with conflicts).

Either way, without the dependencies your module will not start.
Jürgen Untch, modified 5 Years ago. New Member Posts: 21 Join Date: 11/14/19 Recent Posts
Hi David. Thanks for your reply. I removed the compile-scope in the pom above and also modified the bnd file you see below. Now with this configuration the errors are gone. Do you know if there is a more user-friendly way doing this?
Bundle-Name: jasper
Bundle-SymbolicName: com.example.service.jasper
Bundle-Version: 1.0.0
Bundle-Activator: com.example.service.activator.JasperReportServiceActivator
Export-Package: com.example.service.jasper.api
Import-Package: \
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!com.adobe.xmp.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!com.google.zxing.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!com.ibm.icu.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!java.xml.soap.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!jaxen.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!mondrian.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!net.sf.barcode4j.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!net.sourceforge.barbecue.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!net.tascalate.javaflow.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!org.apache.ant.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!org.codehaus.groovy.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!groovy.lang,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!groovyjarjarasm.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!net.sf.jasperreports.annotations.properties,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!net.sf.jasperreports.metadata.properties,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!netscape.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!org.apache.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!org.bouncycastle.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!org.castor.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!org.eclipse.jdt.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!org.exolab.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!org.hibernate.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!org.krysalis.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!org.mozilla.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!org.olap4j.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!org.springframework.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!org.w3c.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;!javax.inject.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;net.sf.jasperreports.*,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;*
Include-Resource: @jasperreports-6.10.0.jar,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;@castor-xml-1.4.1.jar,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;@commons-beanutils-1.9.3.jar,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;@commons-collections4-4.2.jar,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;@commons-digester-2.1.jar,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;@commons-logging-1.1.1.jar,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;@ecj-4.4.2.jar,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;@itext-2.1.7.js7.jar,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;@jcommon-1.0.23.jar,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;@jfreechart-1.0.19.jar,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;@jackson-annotations-2.9.9.jar,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;@jackson-core-2.9.9.jar,\
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;@jackson-databind-2.9.9.jar,\

thumbnail
David H Nebinger, modified 5 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
Nope, that's pretty much it... 
Jürgen Untch, modified 5 Years ago. New Member Posts: 21 Join Date: 11/14/19 Recent Posts
Unfortunately I get further errors. What is this?
2020-04-01 09:43:00.088 ERROR [Framework Event Dispatcher: Equinox Container: f0cdf455-5673-001a-1759-ca29c79c48cd][com_example_service_jasper:97] FrameworkEvent ERROR&nbsp;
java.io.IOException: Exception in opening zip file: /var/liferay/osgi/state/org.eclipse.osgi/92/1/bundleFile
&nbsp;&nbsp; &nbsp;at org.eclipse.osgi.framework.util.SecureAction.getZipFile(SecureAction.java:305)
&nbsp;&nbsp; &nbsp;at org.eclipse.osgi.storage.bundlefile.ZipBundleFile.basicOpen(ZipBundleFile.java:85)
&nbsp;&nbsp; &nbsp;at org.eclipse.osgi.storage.bundlefile.ZipBundleFile.getZipFile(ZipBundleFile.java:98)
&nbsp;&nbsp; &nbsp;at org.eclipse.osgi.storage.bundlefile.ZipBundleFile.checkedOpen(ZipBundleFile.java:65)
&nbsp;&nbsp; &nbsp;at org.eclipse.osgi.storage.bundlefile.ZipBundleFile.getEntry(ZipBundleFile.java:232)
&nbsp;&nbsp; &nbsp;at org.eclipse.osgi.internal.loader.classpath.ClasspathManager.findClassImpl(ClasspathManager.java:562)
&nbsp;&nbsp; &nbsp;at org.eclipse.osgi.internal.loader.classpath.ClasspathManager.findLocalClassImpl(ClasspathManager.java:540)
&nbsp;&nbsp; &nbsp;at org.eclipse.osgi.internal.loader.classpath.ClasspathManager.findLocalClass(ClasspathManager.java:527)
&nbsp;&nbsp; &nbsp;at org.eclipse.osgi.internal.loader.ModuleClassLoader.findLocalClass(ModuleClassLoader.java:324)
&nbsp;&nbsp; &nbsp;at org.eclipse.osgi.internal.loader.BundleLoader.findLocalClass(BundleLoader.java:330)
&nbsp;&nbsp; &nbsp;at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:407)
&nbsp;&nbsp; &nbsp;at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:357)
&nbsp;&nbsp; &nbsp;at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:349)
&nbsp;&nbsp; &nbsp;at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:160)
&nbsp;&nbsp; &nbsp;at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
&nbsp;&nbsp; &nbsp;at java.lang.Class.forName0(Native Method)
&nbsp;&nbsp; &nbsp;at java.lang.Class.forName(Class.java:348)
&nbsp;&nbsp; &nbsp;at org.apache.commons.logging.impl.LogFactoryImpl.createLogFromClass(LogFactoryImpl.java:1081)
&nbsp;&nbsp; &nbsp;at org.apache.commons.logging.impl.LogFactoryImpl.discoverLogImplementation(LogFactoryImpl.java:914)
&nbsp;&nbsp; &nbsp;at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:604)
&nbsp;&nbsp; &nbsp;at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:336)
&nbsp;&nbsp; &nbsp;at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:310)
&nbsp;&nbsp; &nbsp;at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:685)
&nbsp;&nbsp; &nbsp;at net.sf.jasperreports.engine.util.JRLoader.<clinit>(JRLoader.java:81)
&nbsp;&nbsp; &nbsp;at com.example.service.jasper.impl.JasperReportServiceImpl.generatePDFReport(JasperReportServiceImpl.java:42)
&nbsp;&nbsp; &nbsp;at com.example.service.application.impl.ApplicationDocumentJob.handleSendToFavoritRequest(ApplicationDocumentJob.java:115)
&nbsp;&nbsp; &nbsp;at com.example.service.application.impl.ApplicationDocumentJob.process(ApplicationDocumentJob.java:200)
&nbsp;&nbsp; &nbsp;at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
&nbsp;&nbsp; &nbsp;at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
&nbsp;&nbsp; &nbsp;at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
&nbsp;&nbsp; &nbsp;at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
&nbsp;&nbsp; &nbsp;at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
&nbsp;&nbsp; &nbsp;at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
&nbsp;&nbsp; &nbsp;at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
&nbsp;&nbsp; &nbsp;at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
&nbsp;&nbsp; &nbsp;at com.example.service.application.impl.ApplicationDocumentJob.doReceive(ApplicationDocumentJob.java:191)
&nbsp;&nbsp; &nbsp;at com.liferay.portal.kernel.messaging.BaseMessageListener.receive(BaseMessageListener.java:26)
&nbsp;&nbsp; &nbsp;at com.liferay.portal.kernel.scheduler.messaging.SchedulerEventMessageListenerWrapper.receive(SchedulerEventMessageListenerWrapper.java:66)
&nbsp;&nbsp; &nbsp;at com.liferay.portal.kernel.messaging.InvokerMessageListener.receive(InvokerMessageListener.java:74)
&nbsp;&nbsp; &nbsp;at com.liferay.portal.kernel.messaging.ParallelDestination$1.run(ParallelDestination.java:52)
&nbsp;&nbsp; &nbsp;at com.liferay.portal.kernel.concurrent.ThreadPoolExecutor$WorkerTask._runTask(ThreadPoolExecutor.java:756)
&nbsp;&nbsp; &nbsp;at com.liferay.portal.kernel.concurrent.ThreadPoolExecutor$WorkerTask.run(ThreadPoolExecutor.java:667)
&nbsp;&nbsp; &nbsp;at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.FileNotFoundException: /var/liferay/osgi/state/org.eclipse.osgi/92/1/bundleFile (No such file or directory)
&nbsp;&nbsp; &nbsp;at java.util.zip.ZipFile.open(Native Method)
&nbsp;&nbsp; &nbsp;at java.util.zip.ZipFile.<init>(ZipFile.java:225)
&nbsp;&nbsp; &nbsp;at java.util.zip.ZipFile.<init>(ZipFile.java:155)
&nbsp;&nbsp; &nbsp;at java.util.zip.ZipFile.<init>(ZipFile.java:169)
&nbsp;&nbsp; &nbsp;at org.eclipse.osgi.framework.util.SecureAction.getZipFile(SecureAction.java:288)
&nbsp;&nbsp; &nbsp;... 42 more
Exception in thread "liferay/scheduler_dispatch-250" java.lang.ExceptionInInitializerError
&nbsp;&nbsp; &nbsp;at net.sf.jasperreports.engine.util.JRStyledTextParser.<clinit>(JRStyledTextParser.java:86)
&nbsp;&nbsp; &nbsp;at net.sf.jasperreports.engine.fill.JRBaseFiller.<init>(JRBaseFiller.java:116)
&nbsp;&nbsp; &nbsp;at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:79)
&nbsp;&nbsp; &nbsp;at net.sf.jasperreports.engine.fill.JRFiller.createBandReportFiller(JRFiller.java:251)
&nbsp;&nbsp; &nbsp;at net.sf.jasperreports.engine.fill.JRFiller.createReportFiller(JRFiller.java:272)
&nbsp;&nbsp; &nbsp;at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:156)
&nbsp;&nbsp; &nbsp;at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:145)
&nbsp;&nbsp; &nbsp;at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:758)
&nbsp;&nbsp; &nbsp;at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:1074)
&nbsp;&nbsp; &nbsp;at com.example.service.jasper.impl.JasperReportServiceImpl.generatePDFReport(JasperReportServiceImpl.java:50)
&nbsp;&nbsp; &nbsp;at com.example.service.application.impl.ApplicationDocumentJob.handleSendToFavoritRequest(ApplicationDocumentJob.java:115)
&nbsp;&nbsp; &nbsp;at com.example.service.application.impl.ApplicationDocumentJob.process(ApplicationDocumentJob.java:200)
&nbsp;&nbsp; &nbsp;at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
&nbsp;&nbsp; &nbsp;at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
&nbsp;&nbsp; &nbsp;at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
&nbsp;&nbsp; &nbsp;at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
&nbsp;&nbsp; &nbsp;at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
&nbsp;&nbsp; &nbsp;at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
&nbsp;&nbsp; &nbsp;at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
&nbsp;&nbsp; &nbsp;at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
&nbsp;&nbsp; &nbsp;at com.example.service.application.impl.ApplicationDocumentJob.doReceive(ApplicationDocumentJob.java:191)
&nbsp;&nbsp; &nbsp;at com.liferay.portal.kernel.messaging.BaseMessageListener.receive(BaseMessageListener.java:26)
&nbsp;&nbsp; &nbsp;at com.liferay.portal.kernel.scheduler.messaging.SchedulerEventMessageListenerWrapper.receive(SchedulerEventMessageListenerWrapper.java:66)
&nbsp;&nbsp; &nbsp;at com.liferay.portal.kernel.messaging.InvokerMessageListener.receive(InvokerMessageListener.java:74)
&nbsp;&nbsp; &nbsp;at com.liferay.portal.kernel.messaging.ParallelDestination$1.run(ParallelDestination.java:52)
&nbsp;&nbsp; &nbsp;at com.liferay.portal.kernel.concurrent.ThreadPoolExecutor$WorkerTask._runTask(ThreadPoolExecutor.java:756)
&nbsp;&nbsp; &nbsp;at com.liferay.portal.kernel.concurrent.ThreadPoolExecutor$WorkerTask.run(ThreadPoolExecutor.java:667)
&nbsp;&nbsp; &nbsp;at java.lang.Thread.run(Thread.java:748)
</init></init></clinit></init></init></init></clinit>
thumbnail
David H Nebinger, modified 5 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
Shut down Liferay, purge the osgi/state directory, then try again...
Jürgen Untch, modified 5 Years ago. New Member Posts: 21 Join Date: 11/14/19 Recent Posts
Thank you David. Deleting the files in osgi/state, liferay/work, tomcat/work, tomcat/temp and a restart solved the problem.