JAXB in Netbeans Module

冷暖自知 提交于 2020-01-01 09:39:34

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/7189967/jaxb-in-netbeans-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!