Problem with JAXBContext.newInstance

Lance Andersen lance.andersen at oracle.com
Mon Feb 11 23:33:27 UTC 2019


Using the JAX-B standalone,https://github.com/eclipse-ee4j/jaxb-ri, seems a bit harder than it probably should be given where we are. 

The following command line  with the jars on the classpath:
——————
java -cp classes/:jars/jaxb-api-2.4.0.jar:jars/jaxb-runtime-2.4.0.jar:jars/jaxb-impl-2.4.0.jar:jars/istack-commons-runtime-3.0.7.jar:jars/javax.activation-api-1.2.0.jar  JAXBExample
—————————

will work with the following trivial example:

———————
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Customer {

    String name;
    int age;
    int id;

    public String getName() {
        return name;
    }

    @XmlElement
    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    @XmlElement
    public void setAge(int age) {
        this.age = age;
    }

    public int getId() {
        return id;
    }

    @XmlAttribute
    public void setId(int id) {
        this.id = id;
    }

}

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class JAXBExample {
    public static void main(String[] args) {

        Customer customer = new Customer();
        customer.setId(100);
        customer.setName("Lance Andersen");
        customer.setAge(29);

        try {

            File file = new File("Customer.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            // output pretty printed
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            jaxbMarshaller.marshal(customer, file);
            jaxbMarshaller.marshal(customer, System.out);

        } catch (JAXBException e) {
            e.printStackTrace();
        }

    }
}
——————————

The JAX-B 2.4 documentation could be a bit more helpful https://javaee.github.io/jaxb-v2/doc/user-guide/release-documentation.html#deployment-jaxb-on-jpms :-(


HTH

Lance
> On Feb 11, 2019, at 4:46 PM, Oliver Z. <oliver.zemann at gmail.com> wrote:
> 
> Hi, i am trying to create a java 11 application which reads some .xml and that should be mapped to a object. Unfortunately, with java 11 this seems impossible. I created a stackoverflow posting. Maybe someone could take a look at that and help me with my problem. https://stackoverflow.com/questions/54632086/java-11-implementation-of-jaxb-api-has-not-been-found-on-module-path-or-classpa
> 

 <http://oracle.com/us/design/oracle-email-sig-198324.gif>
 <http://oracle.com/us/design/oracle-email-sig-198324.gif> <http://oracle.com/us/design/oracle-email-sig-198324.gif>
 <http://oracle.com/us/design/oracle-email-sig-198324.gif>Lance Andersen| Principal Member of Technical Staff | +1.781.442.2037
Oracle Java Engineering 
1 Network Drive 
Burlington, MA 01803
Lance.Andersen at oracle.com <mailto:Lance.Andersen at oracle.com>





More information about the jigsaw-dev mailing list