Error printing XML created in Java

1

Good morning, I am developing a program that through classes I can generate an XML, this is my code, which DOES NOT have any error, I do not mark error in any line. (I'm developing with xstream bookstores)

Book

package ejemplo.xstream;
public class Libro {
    private String tipo;
    private String titulo;
    private String autor;
    public Libro(String tipo, String titulo, String autor) {
        this.tipo = tipo;
        this.titulo = titulo;
        this.autor = autor;
    }
    public String getTipo() {
        return tipo;
    }
    public void setTipo(String tipo) {
        this.tipo = tipo;
    }
    public String getTitulo() {
        return titulo;
    }
    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }
    public String getAutor() {
        return autor;
    }
    public void setAutor(String autor) {
        this.autor = autor;
    }

}

Library

package ejemplo.xstream;
import java.util.ArrayList;
import java.util.List;
public class Biblioteca {
    List<Libro> libros=new ArrayList<Libro>();
    public Biblioteca(Libro libro) {
        this.libros.add(libro);
    }
    public List<Libro> getLibros() {
        return libros;
    }
    public void add(Libro libro) {
        this.libros.add(libro);
    }
}

StringConverter

package ejemplo.xstream;
import com.thoughtworks.xstream.converters.SingleValueConverter;
public class StringConverter implements SingleValueConverter {
    @Override
    public boolean canConvert(Class arg0) {
        return arg0.equals(String.class);
    }
    @Override
    public Object fromString(String arg0) {
        return arg0;
    }
    @Override
    public String toString(Object arg0) {
        return (String) arg0;
    }

}

XstreamTransformation

package ejemplo.xstream;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class XstreamTransformacion {
    private static XStream configuracion(){
        XStream xstream=new XStream(new DomDriver());
        xstream.alias("biblioteca", Biblioteca.class);
        xstream.alias("libro", Libro.class);
        xstream.addImplicitCollection(Biblioteca.class, "libros");
        xstream.useAttributeFor(Libro.class, "tipo");
        xstream.registerConverter(new StringConverter());   
        return xstream;
    }
    public static String toXML(Biblioteca biblioteca){
        XStream xstream=XstreamTransformacion.configuracion();
        return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+xstream.toXML(biblioteca);
    }             
    public static Biblioteca fromXML(String xml){
        XStream xstream=XstreamTransformacion.configuracion();
        return (Biblioteca)xstream.fromXML(xml);
    }
}

Tests

package ejemplo.xstream;
public class Pruebas {
    public static void main(String[] args) {
        Biblioteca biblioteca=new Biblioteca(new Libro("Novela", "Don quijote", "Miguel de cervantes"));
        biblioteca.add(new Libro("teatro", "Romeo y julieta", "William Shakespeare"));
        biblioteca.add(new Libro("cuento", "El Aleph", "Jorge Luis Borges"));
        biblioteca.add(new Libro("poemas", "Antologia", "Mario Benedetti"));
        String xml=XstreamTransformacion.toXML(biblioteca);
        System.out.println(xml);
    }

}

And it throws me all these errors:

  

Exception in thread "main"   com.thoughtworks.xstream.InitializationException: Could not   instantiate mapper: com.thoughtworks.xstream.mapper.LambdaMapper at   com.thoughtworks.xstream.XStream.buildMapperDynamically (XStream.java:646)     at com.thoughtworks.xstream.XStream.buildMapper (XStream.java:623) at   com.thoughtworks.xstream.XStream. (XStream.java:587) at   com.thoughtworks.xstream.XStream. (XStream.java:515) at   com.thoughtworks.xstream.XStream. (XStream.java:484) at   com.thoughtworks.xstream.XStream. (XStream.java:430) at   com.thoughtworks.xstream.XStream. (XStream.java:397) at   example.xstream.XstreamTransformation.configuration (XstreamTransformacion.java:6)     at   example.xstream.XstreamTransformation.toXML (XstreamTransformacion.java:15)     at example.xstream.Pruebas.main (Tests.java:9) Caused by:   java.lang.ClassNotFoundException:   com.thoughtworks.xstream.mapper.LambdaMapper at   com.thoughtworks.xstream.core.util.CompositeClassLoader.loadClass (CompositeClassLoader.java:148)     at java.lang.Class.forName0 (Native Method) at   java.lang.Class.forName (Unknown Source) at   com.thoughtworks.xstream.XStream.buildMapperDynamically (XStream.java:642)     ... 9 more

What will it be? thanks!

    
asked by Antonio Alejos 29.11.2018 в 18:23
source

2 answers

1

The error is only one, what you see is the stack trace of the execution at the time of failure:

  

Exception in thread "main"    com.thoughtworks.xstream.InitializationException : Could not instantiate mapper: com.thoughtworks.xstream.mapper.LambdaMapper at   com.thoughtworks.xstream.XStream.buildMapperDynamically (XStream.java:646)   at com.thoughtworks.xstream.XStream.buildMapper (XStream.java:623) at   com.thoughtworks.xstream.XStream. (XStream.java:587) at   com.thoughtworks.xstream.XStream. (XStream.java:515) at   com.thoughtworks.xstream.XStream. (XStream.java:484) at   com.thoughtworks.xstream.XStream. (XStream.java:430) at   com.thoughtworks.xstream.XStream. (XStream.java:397) at   example.xstream.XstreamTransformation.configuration (XstreamTransformacion.java:6)   at   example.xstream.XstreamTransformation.toXML (XstreamTransformacion.java:15)   at example.xstream.Pruebas.main (Tests.java:9) Caused by :   java.lang. ClassNotFoundException :   com.thoughtworks.xstream.mapper. LambdaMapper at   com.thoughtworks.xstream.core.util.CompositeClassLoader.loadClass (CompositeClassLoader.java:148)   at java.lang.Class.forName0 (Native Method) at   java.lang.Class.forName (Unknown Source) at   com.thoughtworks.xstream.XStream.buildMapperDynamically (XStream.java:642)   ... 9 more

Look at the bold letters. The xstream library does not find a class at runtime. Make sure the classpath is correct and point to the jar of that library.

    
answered by 29.11.2018 в 18:36
1

I followed these steps and with your code worked correctly.

  • Download the XStream library ( link )
  • Aim at the unzipped jar in the library
  • Upload your classes

This is the console output of the Test class.

Greetings.

    
answered by 17.12.2018 в 20:27