This exception occur when you are using JAXB to marshal a java object (collection type) to xml format. The stack trace looks like this:
Exception in thread "main" javax.xml.bind.JAXBException: class java.util.ArrayList nor any of its super class is known to this context. at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfo(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Unknown Source) at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source) at com.howtodoinjava.jaxb.examples.list.TestEmployeeMarshing.main(TestEmployeeMarshing.java:58)

Reason
The above exception occurs because JAXB always expects a @XmlRootElement annotation on the entity, it gets to marshal. This is mandatory and can not be skipped. This @XmlRootElement annotation is required to get meta data from root element of XML marshalled from java object.
ArrayList class OR any java collection class does not have any JAXB annotations on it. Due to this JAXB is unable to parse any such java objects and raises this error.
Solution : Create wrapper class
This is recommended approach because it gives you flexibility to add/remove fields in future e.g. size attribute.
@XmlRootElement(name = "employees") @XmlAccessorType (XmlAccessType.FIELD) public class Employees { @XmlElement(name = "employee") private List<Employee> employees = null; public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } }
Now you can use this class as below:
static Employees employees = new Employees(); static { employees.setEmployees(new ArrayList<Employee>()); Employee emp1 = new Employee(); emp1.setId(1); emp1.setFirstName("Lokesh"); emp1.setLastName("Gupta"); emp1.setIncome(100.0); Employee emp2 = new Employee(); emp2.setId(2); emp2.setFirstName("John"); emp2.setLastName("Mclane"); emp2.setIncome(200.0); employees.getEmployees().add(emp1); employees.getEmployees().add(emp2); } private static void marshalingExample() throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(Employees.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(employees, System.out); } Output: <employees> <employee> <id>1</id> <firstName>Lokesh</firstName> <lastName>Gupta</lastName> <income>100.0</income> </employee> <employee> <id>2</id> <firstName>John</firstName> <lastName>Mclane</lastName> <income>200.0</income> </employee> </employees>
See the complete example in this post: Marshalling example java ArrayList or Set
Happy Learning !!
Thanks a lot! It was really helpful. Nice and simple.
Thanks it worked for me object needs to be same as your @XmlRootElement(name=”studentList”),List studentList = new ArrayList(); and try to make it generic using your poja class
package com.javainterviewpoint;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name=”studentList”)
public class StudentList
{
List studentList = new ArrayList();
public List getStudentList()
{
return studentList;
}
public void setStudentList(List studentList)
{
this.studentList = studentList;
}
}
Hi,
I have tried your solution, with a bit of modification to suit my use case. However, I am now getting a cast error in my soap: “java.lang.ClassCastException: acme.flight.soap.service.impl.FlightList cannot be cast to java.util.List”
Please let me know if you can help with this and how I can solve it. I have pasted the class below.
FlightList Class
@XmlRootElement(name = “flights”)
@XmlAccessorType(XmlAccessType.FIELD)
public class FlightList {
@XmlElement(name = "flight")
protected List flights = null;
public List getFlights() {
return flights;
}
public void setFlights(List flights) {
this.flights = flights;
}
}
SoapImpl Class
public List getFlightList(String startCity, String destCity, Date departDate, Date returnDate) {
FlightList flightList = new FlightList();
{
flightList.setFlights(new ArrayList());
Flight outbound = new Flight();
Query q1 = em.createNamedQuery("Flight.findFiltered");
q1.setParameter("startCity", startCity);
q1.setParameter("targetCity", destCity);
q1.setParameter("travelDate", departDate);
outbound = (Flight) q1.getSingleResult();
Flight inbound = new Flight();
Query q2 = em.createNamedQuery("Flight.findFiltered");
q2.setParameter("startCity", destCity);
q2.setParameter("targetCity", startCity);
q1.setParameter("travelDate", returnDate);
inbound = (Flight) q2.getSingleResult();
flightList.getFlights().add(inbound);
flightList.getFlights().add(outbound);
}
return (List) flightList;
}
No worries, I have found a solution to my problem. I needed to convert my FlightList class into a list.
These are the changes I did to make it work
FlightList Class
@XmlRootElement(name = “flights”)
@XmlAccessorType(XmlAccessType.FIELD)
public class FlightList extends ArrayList {
}
SoapImpl Class
public List getFlightList(String startCity, String destCity, Date departDate, Date returnDate) {
FlightList flightList = new FlightList();
}
Glad, you figured out.
Exception in thread “main” javax.xml.bind.JAXBException: Unable to create context
– with linked exception:
[java.lang.reflect.InvocationTargetException]
at javax.xml.bind.ContextFinder.find(ContextFinder.java:61)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:77)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:73)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:69)
at com.ds.test.POTest.main(POTest.java:17)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:592)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:59)
… 4 more
Caused by: java.lang.NoSuchMethodError: javax.xml.bind.annotation.XmlAccessorType.value()Ljavax/xml/bind/annotation/AccessType;
at com.sun.xml.bind.v2.model.impl.ClassInfoImpl.getAccessType(ClassInfoImpl.java:339)
at com.sun.xml.bind.v2.model.impl.ClassInfoImpl.getProperties(ClassInfoImpl.java:228)
at com.sun.xml.bind.v2.model.impl.RuntimeClassInfoImpl.getProperties(RuntimeClassInfoImpl.java:87)
at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:127)
at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo(RuntimeModelBuilder.java:49)
at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo(RuntimeModelBuilder.java:41)
at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:189)
at com.sun.xml.bind.v2.model.impl.RegistryInfoImpl.(RegistryInfoImpl.java:51)
at com.sun.xml.bind.v2.model.impl.ModelBuilder.addRegistry(ModelBuilder.java:232)
at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:201)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:327)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.(JAXBContextImpl.java:198)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:76)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:55)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:124)
… 9 more
In JAXB 2.0, AccessType is renamed to XmlAccessType (AccessorType is also renamed to XmlAccessorType). Update source.
you can find a complete example here:
not working for me still the same error occuring 🙁
Give me the code.