users@jaxb.java.net

Re: ClassCastException in Eclipse -> Run

From: Andrew Hughes <azza_at_lisasoft.com>
Date: Fri, 26 May 2006 14:46:09 +0930
Thanks for the reply!


Using This: :-)
JAXBElement element = (JAXBElement)u.unmarshal(myXmlFile);
MyType myType = (MyType) element.getValue();


Rather Than: :'(
myType = (MyType) u.unmarshal(myXmlFile);

Solved the problem!!!!


Thanks very much for the fast response!!!!

I think this would be a good suggestion for an additional note on the FAQ page.  (https://jaxb.dev.java.net/faq/index.html#classloader)

AH



ray lukas wrote:

Well I would not consider my self an expert in this by any stretch but let me try and give you a hand.

Things have changed slightly since the older version of JAXB. I will let the experts explain the changes but here is a chunk of code that should answer (I hope) you questions and get you back on the road to success

 

 

            private EvaluationConfigurationType loadEvaluationConfigFile() throws Exception {

                        if (evalConfig == null) {   

                                    //evalConfigFileWorkVersn <- evalConfigFile

                    JAXBContext jc = JAXBContext.newInstance( "client.database.evaluator.generated" );

                    Unmarshaller u = jc.createUnmarshaller();

                    SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);

                    try {

                        Schema schema = sf.newSchema(new File("knownGoodProcessing\\evalConfig.xsd"));

                        u.setSchema(schema);

                        u.setEventHandler(new Validator());

                    }

                    catch (org.xml.sax.SAXException se) {

                System.out.println("Unable to validate due to following error.");

                se.printStackTrace();

            }

                    JAXBElement jaxbElement = (JAXBElement)u.unmarshal(new FileInputStream(evalConfigFile));

                    System.out.println("u.unmarshal(new FileInputStream(" + evalConfigFile + "))=>" + jaxbElement.getValue().getClass().getName());

                    evalConfig = (EvaluationConfigurationType)jaxbElement.getValue();

                        }

                        return evalConfig;

            }

 

The key point here is that my unmarshall does not return a generated class but instead a JAXBElement from which I can extract my generated class (EvaluationConfigurationType).. Does this help clear things up..

 

 


From: Andrew Hughes [mailto:azza@lisasoft.com]
Sent: Thursday, May 25, 2006 9:46 PM
To: users@jaxb.dev.java.net
Subject: ClassCastException in Eclipse -> Run

 

Hey All,

I've read the FAQ and seen the problems with Class Loaders... I'm invoking my class from inside "
Eclipse -> Run -> Run As: Java Application"

My code is:

JAXBContext jc = JAXBContext.newInstance("com.mypackage",this.getClass().getClassLoader());
Unmarshaller u = jc.createUnmarshaller();
MyClass myXml = (MyClass) u.unmarshal(myXmlFile);

However I get a typical ClassCast Exception:

java.lang.ClassCastException: javax.xml.bind.JAXBElement
    at com.mypackage.TestService.run(TestService.java:24)
    at com.mypackage.MyClass.main(MyClass.java:18)

My symptoms are exactly the same as :  https://jaxb.dev.java.net/faq/index.html#classloader

I would prefer to try and solve this problem rather than having to deploy my code to tomcat all the time, or running from command line (if possible).


Thanks in advance.

AH

--------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@jaxb.dev.java.net For additional commands, e-mail: users-help@jaxb.dev.java.net