0

Running across and Illegal relfective access operation

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.sun.xml.bind.v2.runtime.reflect.opt.Injector (file:/Users/sshams/.m2/repository/com/sun/xml/bind/jaxb-impl/2.2.11/jaxb-impl-2.2.11.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)
WARNING: Please consider reporting this to the maintainers of com.sun.xml.bind.v2.runtime.reflect.opt.Injector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release Exception in thread "main"
javax.xml.bind.JAXBException: class java.util.ArrayList nor any of its super class is known to this context.

Customer class

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType(propOrder = {"name", "phone", "about", "age", "balance", "active", "joined"})
public class Customer {

    private int id;
    private String name;
    private String phone;
    private String about;
    private int age;
    private BigDecimal balance;
    private boolean active;
    private Date joined;

    public Customer() {
    }

    @XmlAttribute
    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAbout() {
        return about;
    }

    public void setAbout(String about) {
        this.about = about;
    }

    public int getAge() {
        return age;
    }

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

    public BigDecimal getBalance() {
        return balance;
    }

    public void setBalance(BigDecimal balance) {
        this.balance = balance;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public Date getJoined() {
        return joined;
    }

    public void setJoined(Date joined) {
        this.joined = joined;
    }
}

Customers class

@XmlRootElement(name = "customers")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Customers {

    @XmlElement(name="customer", type=Customer.class)
    private List<Customer> customers;

    public List<Customer> getCustomers() {
        return customers;
    }

    public void setCustomers(List<Customer> customers) {
        this.customers = customers;
    }
}

Main class

public static void main(String[] args) throws IOException, ParserConfigurationException, JAXBException {
    List<Customer> customers = DataProvider.readDataFromXML("data/customers.xml");

    JAXBContext jaxbContext = JAXBContext.newInstance(Customers.class);
    Marshaller marshaller = jaxbContext.createMarshaller();

    StringWriter stringWriter = new StringWriter();
    marshaller.marshal(customers2, stringWriter);

    System.out.println(stringWriter.toString());
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
Developer
  • 924
  • 3
  • 14
  • 30
  • There is a reason you have a `Customers` *(plural)* class, so why are you not using it? --- And what is `customers2`? I assume it is an `ArrayList`, and you cannot give a list directly to the `marshal` method. What would be the name of the XML root element? – Andreas Apr 20 '19 at 02:24
  • customers2 is customers, sorry a typo – Developer Apr 20 '19 at 02:33

0 Answers0