views:

30

answers:

2

Hi,

I am writing a plugin for Jira which involves parsing of XML documents. I am using JAXB to do so (XML to pojos and vice versa) So have a class which generates XML from pojos using JAXB. it looks like...

import javax.xml.bind.*;

Class Parser {
  public void m1() {
    ...
    // code which uses classes in javax.xml.bind.*
  }

  public static void main(String args[]){
   Parser p=new Parser();
   p.m1();

  } 
}

the mentioned packages will come with JDK distribution (rt.jar). so i haven't relayed on anything else to run the class.

when i launch it from command line using 'java' it's working properly. but, when i package it as a jar and put it as plugin in Jira it's failing with the following error

javax.xml.bind.JAXBException: Provider com.sun.xml.bind.v2.ContextFactory not found
 - with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory]
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:152)
        at javax.xml.bind.ContextFinder.find(ContextFinder.java:299)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:372)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:337)

this is on the same machine. only difference i could see is unlike launching from command line, when i deployed it in Jira, it's not calling the main() but m1() by instantiating.

i am wondering what is happening ! it's on the same machine. i do not know how Jira launches the application (as i am launching from command line).

Thanks

A: 

The com.sun.xml.bind package is part of the JAXB RI (http://jaxb.dev.java.net/), so you probably have that on your classpath somewhere.

Java6 has it's own version of JAXB included, in the com.sun.xml.internal.bind package, so you don't usually need the RI in Java6 .

The RI can made made to work with Java6 , but it's uphill battle, and usually ends up with this sort of problem.

skaffman
nope. i have cleared my CLASSPATH. so far i understand, what's happening is, as i am using 'javax.xml.bind.*' located in rt.jar (probably these classes internally use com.sun.xml.internal.bind.* ). when i run my program thru CL, 'bootstrap Class loader' is loading the required classes so program goes smooth. when i deploy it in Jira(tomcat) some other 'ClassLoader' is trying to load the classes and causing the problem
ernesto
hope i was clear.here is the linkhttp://www.developer.com/java/other/article.php/10936_2248831_2/Java-Class-Loading-The-Basics.htmI really need some help i can't figure out what's going on.
ernesto
A: 

Finally i was able find out the reason.

There are many ClassLoaders involved while loading the plugins in JIRA (Felix). which will not delegate to 'bootstrap' ClassLoader.

and hence the problem.

to know which classloader loded 'JAXBContext' class, use 'JAXBContext.class.getClassLoader()' which will print some Felix ClassLoader. it loaded the class from jaxb-api.jar instead of relying on 'rt.jar' but they implemented the classes slightly different . rt.jar version uses 'com.sun.xml.bind.internal.v2.ContextFactory' where jaxb-api version uses 'com.sun.xml.bind.v2.ContextFactory' .

i am able to solve the issue using overlaoded method of JAXB which will take another parameter as 'ClassLoader'.

umm, it took quite some time. but, i am surprised by the inner details & my ignorance

ernesto
can i vote for myself?
ernesto
umm.. 2 hours ! i love SOF
ernesto
`jaxb-api.jar` is part of the JAXB RI, which was what I suggested earlier, as are the implementation package names.
skaffman
yes, i now understand. Thanks man.
ernesto