Search code examples
xstreamkettle

nodecom.thoughtworks.xstream.mapper.CannotResolveClassException while using xstream under Kettle


I am using XStream under Kettle to deserialize XML to a Java object and it always gives me the exception: nodecom.thoughtworks.xstream.mapper.CannotResolveClassException

Then I tried my code separately from Kettle and as a simple Java application. and it works fine. For example:

public static void main(String[] args) {
    person p = new person("JJ", "MM");
    XStream xstream = new XStream(new DomDriver());
    xstream.alias("personname", person.class);
    String xml = xstream.toXML(p);
    person pp = (person) xstream.fromXML(xml);
    System.out.println(pp.toString());
}


public class person {

    private String firstname;
    private String lastname;

    public person(String first, String last) {
        this.firstname = first;
        this.lastname = last;
    }

    public String getFirstname() {
        return this.firstname;
    }

    public String getLastname() {
        return this.lastname;
    }

    public void setFirstname(String name) {
        this.firstname = name;
    }

    public void setLastname(String name) {
        this.lastname = name;
    }

}

And this code works fine. However, when I move this code into the Kettle plugin it fails at the step to read meta data from the XML file.


Solution

  • I was able to fix the issue. I had to set the class loader for XStream instance I am using to de-serialize the xml string.

    So before calling xstream.fromXml(xml) do this:

     xstream.setClassLoader(person.class.getClassLoader());
    

    This will solve the xstream.mapper.CannotResolveClassException exception. This is really weird. Hope this helps.