kdgwill
kdgwill

Reputation: 2199

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

Upvotes: 7

Views: 3632

Answers (2)

Radim
Radim

Reputation: 4808

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).

Upvotes: 3

kdgwill
kdgwill

Reputation: 2199

I Found that if you instead change the initialization of the JAXBContext to JAXBContext.newInstane(String contextPath,ClassLoader loader) in which the class loader you recieve from the current class your in ie MyClass.class.getClassLoader(). Also instead of a schema you can use a jaxb.index which is just a text file list of the class names that you've augmented for use with jaxb that is inside their same directory. Their should be one for each directory though for me they where all in the same directory. and seperated in the same string in the constructor's context path param with :

HERE

Upvotes: 2

Related Questions