Wasting time with JAXB, ANT and APT

Donnerstag, der 3. Juli 2008, 17:56 Uhr von Ullrich

Hello,

I just wasted 2 days finding a damn problem with the mentioned combination. What i tried to do ist: Using JAXB from within a APT AnnotationProcessor from within ANT.

Now to the problem:

From within my processor i could access the classes in the jaxb jar files (jaxb-api.jar and jaxb-impl.jar) but when i tried to unmarshal some XML file, i got a ClassNotFoundException. See http://pastie.org/227136

I tried thousands of things to fix this. Passing the classpath in multiple ways. Even passing a new bootstrap classpath, but non worked.

After lots of try and error, some conversations on #java@freenode (http://pastie.org/227143) and a look at the JAXB sources i found the solution in passing the classloader of the calling class when instantiating the context:

String contextPath = ObjectFactory.class.getPackage().getName();
JAXBContext c = JAXBContext.newInstance(contextPath, MyClass.class.getClassLoader());

Hope this could help someone and save some time :)

Geschrieben in: Coding | Tags: Schlagwörter:,
0 Trackbacks | Keine Kommentare »

Einen Kommentar abgeben

Bitte beachten Sie: Die Kommentare werden moderiert. Dies kann Ihren Kommentar verzögern. Es besteht aber kein Grund, ihn nochmal abzuschicken.