Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Parser Class loading issues in Karaf

I have an OSGi bundle that does some LDAP operations. It uses Apache Shared Directory to do these operations. I'm using Maven Bundle Plugin to build my bundle. For lack of time and resources, I have to go with in pom.xml, which includes Apache Shared Directory and other jars it depends on, in the generated bundle. One of the dependencies is Xerces, followed by Xml apis. When I include these two jars in the bundle, Karaf throws a ClassCastException:

java.lang.ClassCastException: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl cannot be cast to javax.xml.parsers.DocumentBuilderFactory

Further investigation revealed that the class javax.xml.parsers.DocumentBuilderFactory is being loaded from two jars - Xml-apis.jar that I included in my bundle and the JRE's rt.jar, which is causing the ClassCastException. Since this class is being loaded from rt.jar, I figured I don't need to include Xml-apis.jar in my bundle and removed it. However, now I see the ClassNotFoundException:

Caused by: java.lang.ClassNotFoundException: javax.xml.parsers.DocumentBuilderFactory not found by mybundle.ldap [149]
at org.apache.felix.framework.ModuleImpl.findClassOrResourceByDelegation(ModuleImpl.java:812)[org.apache.felix.framework-3.2.2.jar:]
at org.apache.felix.framework.ModuleImpl.access$400(ModuleImpl.java:72)[org.apache.felix.framework-3.2.2.jar:]
at org.apache.felix.framework.ModuleImpl$ModuleClassLoader.loadClass(ModuleImpl.java:1807)[org.apache.felix.framework-3.2.2.jar:]
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)[:1.6.0_35]

So, if I include the xml-apis.jar, I get the ClassCastException. If I don't include it, I get the ClassNotFoundException. Is there a way I can resolve this issue? Any help would be appreciated.

like image 542
John Smith Avatar asked Jul 17 '26 18:07

John Smith


1 Answers

Import the package javax.xml.parsers.

like image 79
Neil Bartlett Avatar answered Jul 20 '26 07:07

Neil Bartlett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!