I'm getting a class cast exception when trying to marhsal Java object into a string. I've included JAXB-2.1 jar in my lib folder. When deploying to WAS, I've changed the classloader strategy to parent last so that the jar in my local library will be picked up first. But this is still throwing classcast exception with the following message. What is the reason for this error?
javax.xml.bind.JAXBException: ClassCastException: attempting to cast jar:file:/opt/was7/base/crm/java/jre/lib/rt.jar!/javax/xml/bind/JAXBContext.class to wsjar:file:/prod/wesadm/wes/was7/base/profiles/sadasd/installedApps/asdadad/myapp.ear/myapp_war.war/WEB-INF/lib/jaxb-api-2.1.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:214)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:372)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522)
at com.my.MyClass.convertObjectToXML()
This is the convertObjectToXML() method.
private <T> String convertObjectToXMLString(T obj) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = jaxbContext.createMarshaller();
StringWriter sw = new StringWriter();
marshaller.marshal(obj, sw);
return sw.toString();
}
This logic works fine when deployed to Tomcat. I'm unable to figure out why WAS jars are being picked up even though I mentioned the class loader as parent last.