Ruben Laguna's blog

Sep 28, 2009 - 1 minute read - Comments - cce classcastexception java jaxb jaxbcontext netbeans parser woodstox

Netbeans & woodstox: ClassCastException: JAXBContextImpl cannot be cast to JAXBContext

Are you getting a ClassCastException in a Netbeans Platform Application, when you are you sure that the class can be casted? Then check http://wiki.netbeans.org/PlainView.jsp?page=DevFaqModuleCCE. This is most likely the result of the the classes being loaded by different classloaders. So JAXBContextImpl cannot be cast to JAXBContext because that JAXBContext is from Classloader “A” and JAXBContextImplcan only be cast to JAXBContextfrom Classloader “B”.

SEVERE [global]
java.lang.ClassCastException: com.sun.xml.bind.v2.runtime.JAXBContextImpl cannot be cast to javax.xml.bind.JAXBContext
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:145)
        at javax.xml.bind.ContextFinder.find(ContextFinder.java:277)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:372)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:337)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:244)
        at com.rubenlaguna.en4j.mainmodule.ImportEvernoteFile.actionPerformed(ImportEvernoteFile.java:65)
        at org.openide.awt.AlwaysEnabledAction.actionPerformed(AlwaysEnabledAction.java:115)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.AbstractButton.doClick(AbstractButton.java:389)
        at com.apple.laf.ScreenMenuItem.actionPerformed(ScreenMenuItem.java:95)
        at java.awt.MenuItem.processActionEvent(MenuItem.java:627)
        at java.awt.MenuItem.processEvent(MenuItem.java:586)
        at java.awt.MenuComponent.dispatchEventImpl(MenuComponent.java:317)
        at java.awt.MenuComponent.dispatchEvent(MenuComponent.java:305)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:638)
        at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:104)
[catch] at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
BUILD SUCCESSFUL (total time: 28 minutes 5 seconds)

So this could happen for example if you have both woodstox in a Netbeans Library Wrapper module and you make you application dependent on ide11JAXB 2.1 Library jaxb21.

In my case I was able to solve by just removing JAXB 2.1 Library from the application.