users@jaxb.java.net

RE: ClassCastException in Eclipse -> Run

From: ray lukas <ray_lukas_at_comcast.net>
Date: Fri, 26 May 2006 05:13:14 -0400

 Oh right.. That's great I finally got one right! Yippee!! Just kidding..

 

I do not know how to add this to the FAQ I think that Kohsuke, the true
master of JAXB, has to get involved on that front.

Have fun let us know if you need any more help.

Ray lukas

(jaxb forever)!

  _____

From: Andrew Hughes [mailto:azza_at_lisasoft.com]
Sent: Friday, May 26, 2006 1:16 AM
To: users_at_jaxb.dev.java.net
Subject: Re: ClassCastException in Eclipse -> Run

 

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_at_lisasoft.com]
Sent: Thursday, May 25, 2006 9:46 PM
To: users_at_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_at_jaxb.dev.java.net For additional
commands, e-mail: users-help_at_jaxb.dev.java.net

 

--------------------------------------------------------------------- To
unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net For additional
commands, e-mail: users-help_at_jaxb.dev.java.net