javajsonjaxbunmarshallingmoxy

JAXB/Moxy unmarshalling JSON runs into error Exception in thread "main" java.lang.NoClassDefFoundError: jakarta/json/JsonException


I am trying to Unmarshal a simple JSON file to my Student POJO using the JAXB/Moxy but I am running into the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: jakarta/json/JsonException
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.getNewXMLReader(SAXUnmarshaller.java:211)
    at com.newJAXB.UnmarshallingJSON.main(UnmarshallingJSON.java:24)
Caused by: java.lang.ClassNotFoundException: jakarta.json.JsonException
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)

If I try to replace the JSON with XML then everything works fine. I get error only when I run Unmarshalling for JSON file.

I just want to know why I am facing the issue with JSON when everything works fine with XML and I am following the steps mentioned in the article. Any help would be really appreciated.

I have Jaxb.properties file with:

jakarta.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Following is my Student class:

@XmlRootElement(name = "Student")
@XmlType(name = "Student", propOrder = {"name", "age"})
@XmlAccessorType(XmlAccessType.FIELD)
@ToString(callSuper = true)
@Data
@NoArgsConstructor
public class Student {
    private String name;
    private String age;
}

Following is my JSON file:

{
  "name": "xyz",
  "age": "25"
}

Following is my Unmarshalling main class:

import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
import org.eclipse.persistence.jaxb.UnmarshallerProperties;

import java.io.InputStream;

public class UnmarshallingJSON {
    public static void main(String[] args) throws JAXBException {
        final InputStream inputStream = com.newJAXB.UnmarshallingJSON.class.getClassLoader().getResourceAsStream("student.json");
        JAXBContext jc = JAXBContext.newInstance(Student.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setProperty("eclipselink.media-type", "application/json");
        unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
        Student student = (Student) unmarshaller.unmarshal(inputStream);
        System.out.println(student);
    }
}

Following are the dependency I have:

        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.moxy</artifactId>
            <version>3.1.0-SNAPSHOT</version>
        </dependency>

I just want to know why I am facing the issue with JSON when everything works fine with XML and I am following the steps mentioned in the article. Any help would be really appreciated.

I tried all methods available on other posts here nothing worked so posting the same.

I am following these articles: https://dzone.com/articles/eclipselink-moxy-and-java-api http://blog.bdoughan.com/2013/07/eclipselink-moxy-and-java-api-for-json.html


Solution

  • The exception actually tells you the issue. You are missing a class at runtime so you need to add a dependency. From the package name and the description of your issue we can assume you want to add Jakarta JSON Processing (JSON-P). Try adding the following dependency

    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>jakarta.json</artifactId>
        <version>2.0.1</version>
    </dependency>