Problem with OSGI import in external jar

Andrzej Pasterczyk
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 2, 2012

I have a bit of a problem with my plugin. Let's say I create my plugin A, in my pom.xml I have defined a dependency on B which is a jar with external API. Everything is nice and my B.jar gets included in A.jar under META-INF\lib\B.jar

The problem is that B.jar uses javax.xml.parsers - probably would be quite easy to solve it would be a dependency somewhere... instead seems to me that someone just copied over the entire jaxp code into B.jar so the jar archives are in general like:

A.jar (my plugin)
- com
- something
- META-INF
- lib
- B.jar

B.jar (jar that I use)
- com
- otherthing
- ClassB.class
- javax
- xml
- parsers
- DocumentBuilderFactory.class

Then in ClassB.class they try to do something with XML which goes to DocumentBuilderFactory.class where the following code

  public static DocumentBuilderFactory newInstance()
  {
    try
    {
      return (DocumentBuilderFactory)FactoryFinder.find("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
    }
    catch (FactoryFinder.ConfigurationError localConfigurationError)
    {
    }
    throw new FactoryConfigurationError(localConfigurationError.getException(), localConfigurationError.getMessage());
  }

returns org.apache.xerces.jaxp.DocumentBuilderFactoryImpl while in ClassB.class they expect it to be javax.xml.parsers.DocumentBuilderFactory so I get to a

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

The obvious question is how do I deal with that?

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted
Andrzej Pasterczyk
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 2, 2012

Took me a while but I got it.

java.xml.parsers.* is already bundled with Jira. There's a conflict since B.jar contains same package/classes. Had few ideas on hiw to fix this but none of them worked. At the end I've thought about using the advantage of JAR files being just archives and I've just deleted the java/xml/parsers/* using WinRAR.

I never thought it will be a working solution (especially that the contents were digitaly signed) but turns out just fine :)

Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 4, 2012

You can achieve this same solution without resorting to manipulating the JAR contents by configuring your maven POM to use dependency exclusions - http://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html

Andrzej Pasterczyk
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 4, 2012

I'm quite sure I've tried that without any luck.

I'm not too familiar with java but the thing is that the B.jar file that I use does not have a dependency on some other jar... it's a part of it, one uber jar, all "dependency" classes are relocated to this one (I guess that this http://maven.apache.org/plugins/maven-shade-plugin/ is one way of getting such package). Are you sure this is going to work in my case?

TAGS
AUG Leaders

Atlassian Community Events