Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB in Netbeans Module

Their Seems to be a problem when i attempt to run JAXB marshaller in a netbeans module. Originally I thought it was the node implimentation so i spent a couple of days reorganizing everything however I was still recieveing the odd error message

javax.xml.bind.JAXBException: ClassCastException: attempting to cast jar:file:/C:/Program%20Files/jmonkeyplatform/ide/modules/ext/jaxb/api/jaxb-api.jar!/javax/xml/bind/JAXBContext.class to jar:file:/C:/Program%20Files/Java/jdk1.6.0_21/jre/lib/rt.jar!/javax/xml/bind/JAXBContext.class.  Please make sure that you are specifying the proper ClassLoader.    
    at javax.xml.bind.ContextFinder.handleClassCastException(ContextFinder.java:96)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:205)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:363)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522)
    at com.spectre.util.JAXBImporterExporter.write(JAXBImporterExporter.java:63)

I am not exactly sure what the issue is the importer/exporter seems to work in normal projects and the importer seems to work fine when parsing the file however the export seems to cause issues. The method I use to export is

 public static <T> void write(T savable, Class<T> type,Object path) {
        try {
            JAXBContext jc = JAXBContext.newInstance(type);
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            if(path instanceof File)
                marshaller.marshal(savable, (File)path);
            else if(path instanceof OutputStream){
                marshaller.marshal(savable, (OutputStream)path);
            }else throw new NoSuchMethodException("The Field Path must be of either type File or OutputStream");
        } catch (NoSuchMethodException ex) {
            Exceptions.printStackTrace(ex);
        } catch (JAXBException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

any assistance is appreciated

like image 333
kdgwill Avatar asked Aug 25 '11 12:08

kdgwill


1 Answers

An easy solution is to add module dependency on org.netbeans.modules.xml.jaxb.api module that is part of NetBeans. This will avoid clash between two versions JAXB classes (one from JDK and second from that module that is preferred in runtime).

like image 61
Radim Avatar answered Oct 13 '22 11:10

Radim