users@jaxb.java.net

Re: Technical Help Required in configuring JAXB in WebApp

From: Kedar Bapat <k_bapat_at_yahoo.com>
Date: Fri, 09 May 2003 09:30:07 -0700

Thanks Kohsuke,

I was also researching on the ClassLoader and my
problem got resolved now....
I feel that if u can document this with the
diagram(very important and helpful to undertand) it
will help other fellow engineers.....

Once again, thanks a lot for u r timely help....

Regards,
Kedar Bapat
Ca, USA
--- Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
wrote:
>
> Let me know if the following document helps (or even
> better, let me know
> how I can improve this document.)
>
> I'm wondering if there's any value in making this
> document publicly
> available. Your case is probably what I'm trying to
> cover, so I'd like
> to know whether you find it useful or not.
>
> regards,
> --
> Kohsuke KAWAGUCHI 408-276-7063
> (x17063)
> Sun Microsystems
> kohsuke.kawaguchi_at_sun.com
>

> ATTACHMENT part 2 image/gif name=classLoaderFAQ.gif

---------------------------------
    Unable to locate jaxb.properties Q.
I'm seeing "Unable to locate jaxb.propertiesfor
package aaa.bbb.ccc."
        Q. I can't cast the unmarshalled objectinto
the generated type.
                             When you invoke
JAXBContext.newInstance("aaa.bbb.ccc"),it tries to
load a property file called jaxb.properties fromeach
package, using the same classloader used to load the
JAXBContextclass itself. This classloader may be
different from the classloader whichwas used to load
your application (see the picture). In this case,
you'llsee the above error. This problem is often seen
with application servers,J2EE containers, Ant, JUnit,
and other applications that use sophisticatedclass
loading mechanisms.


             With some applications, things get even
more complicated when the JAXB-generatedcode can be
loaded by either of classloader. In this case,
JAXBContext.newInstance("aaa.bbb.ccc")will work but
the JVM ends up loading two copies of the generated
classesfor each class loader. As a result,
unmarshalling works but an attempt tocast the returned
object into the expected type will fail, even though
itsgetClass().getName() returns the expected name.



The solution for both situations is to pass your
curent class loader likethis:

    JAXBContext.newInstance( "aaa.bbb.ccc",
this.getClass().getClassLoader() );


In general, if you are writing code that uses JAXB, it
is always betterto explicitly pass in a class loader,
so that your code will work no matterwhere it is
deployed.




__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com