Quick revision
The short answer
(a) means JAXB mapped both the field and its getter as two properties named id; we fix it with @XmlAccessorType(XmlAccessType.FIELD) or by annotating only one of them. (b) means the JAXB API jar is present but no implementation is; we add a runtime such as org.glassfish.jaxb:jaxb-runtime (or MOXy) matching the jakarta or javax namespace. (c) means JAXB cannot create the class because it lacks a no-arg constructor; we add one, even a private one, or map the class through an XmlAdapter.
What to remember
In this answer, we traced each JAXB error to a rule. The duplicate property error comes from the default PUBLIC_MEMBER access type mapping both an annotated field and its getter, fixed by FIELD access or one annotation. The missing implementation error means the API jar is there without a runtime, fixed by adding jaxb-runtime or MOXy for the matching namespace. The missing constructor error means JAXB cannot create the object, fixed by a no-arg constructor, which may be private, or an XmlAdapter. Mixed javax and jakarta jars and interface-typed properties cause similar failures and have similar fixes.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
(a) means JAXB mapped both the field and its getter as two properties named id; we fix it with @XmlAccessorType(XmlAccessType.FIELD) or by annotating only one of them. (b) means the JAXB API jar is present but no implementation is; we add a runtime such as org.glassfish.jaxb:jaxb-runtime (or MOXy) matching the jakarta or javax namespace. (c) means JAXB cannot create the class because it lacks a no-arg constructor; we add one, even a private one, or map the class through an XmlAdapter.
Why this question is asked
These three errors are among the first things anyone meets when they start using JAXB, and they show up again during migrations to Java 11 and later or to Jakarta EE. They look cryptic, but each one points to a simple rule about how JAXB discovers properties, finds its implementation and creates objects. Interviewers like this question because a candidate who has really used JAXB can explain the cause, not only the fix. In this answer, we will recap how JAXB works, then reproduce and fix each error in turn, and finish with two related traps: mixing the javax and jakarta packages, and trying to map interfaces.
1A Quick Recap of How JAXB Reads a Class
JAXB, now Jakarta XML Binding, maps Java classes to XML. When we call JAXBContext.newInstance(Order.class), JAXB inspects the class by reflection, which means reading its fields, methods and annotations at run time, and builds a model of its properties. A property is one named value that becomes one XML element or attribute.
Three rules from that process explain all three errors. First, JAXB decides which fields and methods are properties according to the access type, and the default, PUBLIC_MEMBER, takes every public getter and setter pair plus every public field plus anything annotated. Second, the JAXB API is only a set of interfaces, so at run time it must find an implementation. Third, when reading XML, JAXB has to create an empty instance first and then fill in its properties, so it needs a constructor without arguments.
2Error (a): Class Has Two Properties of the Same Name "id"
Here is a class that triggers the first error.
| Access type | Fields mapped | Getter/setter pairs mapped |
|---|---|---|
PUBLIC_MEMBER (default) | public ones and annotated ones | public ones and annotated ones |
| FIELD | every non-static, non-transient field | only annotated ones |
| PROPERTY | only annotated ones | every pair |
| NONE | only annotated ones | only annotated ones |
The reason is the default access type. Because the field carries @XmlElement, JAXB counts it as a property. Because getId and setId are a public pair, JAXB counts them as a property too. Both are called id, so JAXB refuses to guess which one we meant.
The cleanest fix is to tell JAXB to use fields only, by putting @XmlAccessorType(XmlAccessType.FIELD) on the class. Then getters are ignored unless annotated, and the field is the single source of the mapping. The other fix is to keep the default and move the annotation from the field to the getter, so only the getter pair counts. What we should not do is annotate both.
3Error (b): Implementation of JAXB-API Has Not Been Found
The second error appears at the moment we create a JAXBContext, usually after moving to Java 11 or later, where JAXB is no longer part of the JDK.
The API jar defines JAXBContext, but JAXBContext.newInstance only looks for a provider; it cannot do any binding itself. When the lookup finds nothing, we get this message. Adding a runtime fixes it. The version must match the namespace: jaxb-runtime 3.x and 4.x implement jakarta.xml.bind, while the 2.3 line implements javax.xml.bind. MOXy from EclipseLink is an alternative provider. In Spring Boot, the dependency management already picks matching versions, so we usually add the runtime without a version.
4Error (c): Does Not Have a No-Arg Default Constructor
The third error comes from a class that can only be built through a constructor with arguments.
When unmarshalling, JAXB creates an empty object and then sets each property, so it needs a constructor it can call without arguments. It does not have to be public, which lets us keep our public API unchanged.
When we cannot change the class, for example an immutable type from another library, we use an XmlAdapter. An adapter converts between the awkward type and a simple type that JAXB can handle, such as a String or a small helper class, and we attach it with @XmlJavaTypeAdapter on the field.
5Two Related Traps: Mixed Namespaces and Interfaces
A frequent cause of strange variants of these errors is mixing the two namespaces. If our classes use jakarta.xml.bind annotations but the runtime on the classpath is the old javax one, or the other way round, JAXB simply does not see our annotations. It then falls back to its defaults, which can produce duplicate property errors or ignore our names entirely. The fix is to align the whole set: annotations, API jar and runtime must all be jakarta, or all javax. A quick mvn dependency:tree search for javax.xml.bind usually finds the stray jar.
The other trap is interfaces. JAXB needs a concrete class it can instantiate, so a property typed as an interface fails with a message saying JAXB cannot handle interfaces. We either use a concrete class for the property, tell JAXB which class to use with @XmlElement(type = OrderImpl.class), or write an XmlAdapter between the interface and a concrete class.
6Follow-up questions
Why does @XmlTransient on the field not fix error (a) by itself?
Marking the field @XmlTransient removes the field property, so only the getter pair remains, which does fix the duplicate. But it is easy to forget on the next field, so putting @XmlAccessorType(XmlAccessType.FIELD) at class or package level is the more durable rule. With package-info.java we can set it once for every class in the package.
Why did these errors only appear after upgrading Java?
Up to Java 10, the JDK shipped JAXB, so the API and runtime were always present and matched. Java 11 removed it. From then on, applications must bring their own API and runtime, which is when missing runtimes and mismatched namespaces start to show.
Can JAXB work with records?
Records have no no-arg constructor and no setters, so classic JAXB implementations do not map them directly. The usual approach is an XmlAdapter between the record and a small mutable class. Check your runtime version, since support for newer Java types changes over time.
7Conclusion
In this answer, we traced each JAXB error to a rule. The duplicate property error comes from the default PUBLIC_MEMBER access type mapping both an annotated field and its getter, fixed by FIELD access or one annotation. The missing implementation error means the API jar is there without a runtime, fixed by adding jaxb-runtime or MOXy for the matching namespace. The missing constructor error means JAXB cannot create the object, fixed by a no-arg constructor, which may be private, or an XmlAdapter. Mixed javax and jakarta jars and interface-typed properties cause similar failures and have similar fixes.
Open with
All three come from how JAXB discovers and creates objects. For (a), the default access type is PUBLIC_MEMBER, so JAXB maps both the annotated private field and the public getter and setter pair, and it ends up with two properties called id.
Then, if they want more
I fix it by putting @XmlAccessorType(XmlAccessType.FIELD) on the class or in package-info, or by annotating only the getter. For (b), the JAXB API jar is just interfaces; JAXBContext looks for an implementation and finds none, which typically happens after Java 11 removed JAXB from the JDK.
I add org.glassfish.jaxb:jaxb-runtime, version 4 for jakarta.xml.bind or 2.3 for javax, or use MOXy instead. For (c), unmarshalling creates an empty instance and then sets properties, so JAXB needs a no-arg constructor. It can be private, so the public API does not change, and if I cannot touch the class I map it through an XmlAdapter.
I would also mention two related causes: mixing javax and jakarta jars, which makes JAXB ignore annotations, so the whole dependency set must use one namespace, and interface-typed properties, which need a concrete class or an adapter.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 11, Java 10
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain each JAXB error and its fix: (a) 'IllegalAnnotationsException: Class has two...": https://scrivio-interview-library.netlify.app/java/jaxb/4b0a7ec8e0/