引起的:. lang。ClassNotFoundException:com.sun.xml.bind.v2.model.annotation.AnnotationReader

时间:2022-10-24 19:38:36

You might think that following issue is very simple, but I don't know what I did wrong here. I feel I added required dependencies. Haven't I?

你可能认为下面这个问题很简单,但我不知道我做错了什么。我觉得我添加了必需的依赖项。没有我?

Could anyone please suggest me whats wrong here?

谁能告诉我这里有什么问题吗?

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/xml/bind/v2/model/annotation/AnnotationReader
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:242)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:441)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
    at com.misc.common.ReadXMLFileJaxb.main(ReadXMLFileJaxb.java:14)
Caused by: java.lang.ClassNotFoundException: com.sun.xml.bind.v2.model.annotation.AnnotationReader
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 23 more

Book.java

Book.java

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Book {
    @XmlAttribute
    private String firstName;
    @XmlElement
    private String lastName;
    @XmlElement
    private String age;


    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Book [firstName=" + firstName + ", lastName=" + lastName+ ", age=" + age + "]";
    }
}

ReadXMLFileJaxb.java

ReadXMLFileJaxb.java

public class ReadXMLFileJaxb {
    public static void main(String[] args) {
        File file = new File(ReadXMLFileDOM.class.getClassLoader().getResource("book.xml").getFile());

        try {
            JAXBContext context = JAXBContext.newInstance(Book.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            Book book = (Book) unmarshaller.unmarshal(file);
            System.out.println(book.toString());
        } catch (JAXBException e) {
            System.out.println(e.getMessage());
        }
    }
}

My book.xml

我book.xml

<?xml version="1.0"?>
<book>
    <person>
        <first>Kiran</first>
        <last>Pai</last>
        <age>22</age>
    </person>
    <person>
        <first>Bill</first>
        <last>Gates</last>
        <age>46</age>
    </person>
    <person>
        <first>Steve</first>
        <last>Jobs</last>
        <age>40</age>
    </person>
</book>

引起的:. lang。ClassNotFoundException:com.sun.xml.bind.v2.model.annotation.AnnotationReader

pom.xml

pom.xml

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- JAXB -->
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.12</version>
        </dependency>
    </dependencies>

3 个解决方案

#1


22  

As per link: Why has AnnotationReader been removed from JAXB reference implementation?, you need to simply use below maven dependencies:

根据链接:为什么从JAXB引用实现中删除了AnnotationReader ?您需要简单地使用以下maven依赖项:

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.2.11</version>
</dependency>

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>2.2.11</version>
</dependency>

You need to refactor code a bit. Also looks like you've not created same name fields of model class, it should be like below: Person.java

您需要对代码进行重构。看起来您还没有创建模型类的相同名称字段,应该如下所示:Person.java

@XmlRootElement(name="Person")
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
    @XmlElement
    private String first;
    @XmlElement
    private String last;
    @XmlElement
    private String age;
    public String getFirst() {
        return first;
    }
    public void setFirst(String first) {
        this.first = first;
    }
    public String getLast() {
        return last;
    }
    public void setLast(String last) {
        this.last = last;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person [first=" + first + ", last=" + last + ", age=" + age + "]";
    }
} 

Book.java

Book.java

@XmlRootElement(name="book")
@XmlAccessorType(XmlAccessType.FIELD)
public class Book {
    private List<Person> person = new ArrayList<Person>();

    public List<Person> getPerson() {
        return person;
    }

    public void setPerson(List<Person> person) {
        this.person = person;
    }
}

ReadXMLFileJaxb.java

ReadXMLFileJaxb.java

public class ReadXMLFileJaxb {
    public static void main(String[] args) {
        File file = new File(ReadXMLFileDOM.class.getClassLoader().getResource("book.xml").getFile());

        try {
            JAXBContext context = JAXBContext.newInstance(Book.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            Book book = (Book) unmarshaller.unmarshal(file);
            System.out.println(book.getPerson().size());

            for (int i = 0; i < book.getPerson().size(); i++) {
                System.out.println("------------");
                System.out.println(book.getPerson().get(i).getFirst());
                System.out.println(book.getPerson().get(i).getLast());
                System.out.println(book.getPerson().get(i).getAge());
            }

        } catch (JAXBException e) {
            System.out.println(e.getMessage());
        }
    }
}

The below output I see:

我看到的输出如下:

3
------------
Kiran
Pai
22
------------
Bill
Gates
46
------------
Steve
Jobs
40

#2


6  

Although Issue is very old but still answering. Root cause is that com.sun.xml.bind is obsolete now. org.glassfish.jaxb is latest reference implementation of JAXB API. Using below JAXB RI maven dependency would solve the issue.

虽然问题很古老,但仍在回答。根本原因是com.sun.xml。结合现在已经过时了。org.glassfish。jaxb是jaxb API的最新参考实现。使用以下JAXB RI maven依赖项将解决这个问题。

    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
        <version>2.2.11</version>
    </dependency>

#3


0  

You seem to be running it inside the IDE. For some strange reasons, although the JAXB classes are included in JRE's rt.jar,

您似乎正在IDE中运行它。出于一些奇怪的原因,尽管JAXB类包含在JRE的rt.jar中,

➜  lib jar tvf rt.jar| grep AnnotationReader
  4199 Fri Jan 29 15:35:18 PST 2016 com/sun/xml/internal/bind/v2/model/annotation/AbstractInlineAnnotationReaderImpl.class
  3140 Fri Jan 29 15:35:14 PST 2016 com/sun/xml/internal/bind/v2/model/annotation/AnnotationReader.class
   442 Fri Jan 29 15:35:14 PST 2016 com/sun/xml/internal/bind/v2/model/annotation/RuntimeAnnotationReader.class
  9846 Fri Jan 29 15:35:22 PST 2016 com/sun/xml/internal/bind/v2/model/annotation/RuntimeInlineAnnotationReader.class
  1217 Fri Jan 29 15:35:26 PST 2016 com/sun/xml/internal/ws/model/ReflectAnnotationReader$1.class
  1332 Fri Jan 29 15:35:26 PST 2016 com/sun/xml/internal/ws/model/ReflectAnnotationReader$2.class
  1278 Fri Jan 29 15:35:26 PST 2016 com/sun/xml/internal/ws/model/ReflectAnnotationReader$3.class
  1166 Fri Jan 29 15:35:26 PST 2016 com/sun/xml/internal/ws/model/ReflectAnnotationReader$4.class
  3563 Fri Jan 29 15:35:26 PST 2016 com/sun/xml/internal/ws/model/ReflectAnnotationReader.class

they are not available for your app at runtime.

在运行时,你的应用程序不能使用它们。

However, if I run the app this way:

但是,如果我这样运行app:

➜  target java -cp ./classes com.misc.common.ReadXMLFileJaxb
Book [firstName=null, lastName=Blo, age=33]

i.e. from the command line using the standard Maven structure, it does seem to run.

也就是说,从使用标准Maven结构的命令行中,它似乎确实在运行。

Your book.xml is somewhat wrong, I believe. It contains an embedded person element whereas the Book.java does not. You may want to fix that.

你的书。我认为xml有点不对。它包含一个嵌入的person元素,而这本书。java不。你可能想要解决这个问题。

#1


22  

As per link: Why has AnnotationReader been removed from JAXB reference implementation?, you need to simply use below maven dependencies:

根据链接:为什么从JAXB引用实现中删除了AnnotationReader ?您需要简单地使用以下maven依赖项:

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.2.11</version>
</dependency>

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>2.2.11</version>
</dependency>

You need to refactor code a bit. Also looks like you've not created same name fields of model class, it should be like below: Person.java

您需要对代码进行重构。看起来您还没有创建模型类的相同名称字段,应该如下所示:Person.java

@XmlRootElement(name="Person")
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
    @XmlElement
    private String first;
    @XmlElement
    private String last;
    @XmlElement
    private String age;
    public String getFirst() {
        return first;
    }
    public void setFirst(String first) {
        this.first = first;
    }
    public String getLast() {
        return last;
    }
    public void setLast(String last) {
        this.last = last;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person [first=" + first + ", last=" + last + ", age=" + age + "]";
    }
} 

Book.java

Book.java

@XmlRootElement(name="book")
@XmlAccessorType(XmlAccessType.FIELD)
public class Book {
    private List<Person> person = new ArrayList<Person>();

    public List<Person> getPerson() {
        return person;
    }

    public void setPerson(List<Person> person) {
        this.person = person;
    }
}

ReadXMLFileJaxb.java

ReadXMLFileJaxb.java

public class ReadXMLFileJaxb {
    public static void main(String[] args) {
        File file = new File(ReadXMLFileDOM.class.getClassLoader().getResource("book.xml").getFile());

        try {
            JAXBContext context = JAXBContext.newInstance(Book.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            Book book = (Book) unmarshaller.unmarshal(file);
            System.out.println(book.getPerson().size());

            for (int i = 0; i < book.getPerson().size(); i++) {
                System.out.println("------------");
                System.out.println(book.getPerson().get(i).getFirst());
                System.out.println(book.getPerson().get(i).getLast());
                System.out.println(book.getPerson().get(i).getAge());
            }

        } catch (JAXBException e) {
            System.out.println(e.getMessage());
        }
    }
}

The below output I see:

我看到的输出如下:

3
------------
Kiran
Pai
22
------------
Bill
Gates
46
------------
Steve
Jobs
40

#2


6  

Although Issue is very old but still answering. Root cause is that com.sun.xml.bind is obsolete now. org.glassfish.jaxb is latest reference implementation of JAXB API. Using below JAXB RI maven dependency would solve the issue.

虽然问题很古老,但仍在回答。根本原因是com.sun.xml。结合现在已经过时了。org.glassfish。jaxb是jaxb API的最新参考实现。使用以下JAXB RI maven依赖项将解决这个问题。

    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
        <version>2.2.11</version>
    </dependency>

#3


0  

You seem to be running it inside the IDE. For some strange reasons, although the JAXB classes are included in JRE's rt.jar,

您似乎正在IDE中运行它。出于一些奇怪的原因,尽管JAXB类包含在JRE的rt.jar中,

➜  lib jar tvf rt.jar| grep AnnotationReader
  4199 Fri Jan 29 15:35:18 PST 2016 com/sun/xml/internal/bind/v2/model/annotation/AbstractInlineAnnotationReaderImpl.class
  3140 Fri Jan 29 15:35:14 PST 2016 com/sun/xml/internal/bind/v2/model/annotation/AnnotationReader.class
   442 Fri Jan 29 15:35:14 PST 2016 com/sun/xml/internal/bind/v2/model/annotation/RuntimeAnnotationReader.class
  9846 Fri Jan 29 15:35:22 PST 2016 com/sun/xml/internal/bind/v2/model/annotation/RuntimeInlineAnnotationReader.class
  1217 Fri Jan 29 15:35:26 PST 2016 com/sun/xml/internal/ws/model/ReflectAnnotationReader$1.class
  1332 Fri Jan 29 15:35:26 PST 2016 com/sun/xml/internal/ws/model/ReflectAnnotationReader$2.class
  1278 Fri Jan 29 15:35:26 PST 2016 com/sun/xml/internal/ws/model/ReflectAnnotationReader$3.class
  1166 Fri Jan 29 15:35:26 PST 2016 com/sun/xml/internal/ws/model/ReflectAnnotationReader$4.class
  3563 Fri Jan 29 15:35:26 PST 2016 com/sun/xml/internal/ws/model/ReflectAnnotationReader.class

they are not available for your app at runtime.

在运行时,你的应用程序不能使用它们。

However, if I run the app this way:

但是,如果我这样运行app:

➜  target java -cp ./classes com.misc.common.ReadXMLFileJaxb
Book [firstName=null, lastName=Blo, age=33]

i.e. from the command line using the standard Maven structure, it does seem to run.

也就是说,从使用标准Maven结构的命令行中,它似乎确实在运行。

Your book.xml is somewhat wrong, I believe. It contains an embedded person element whereas the Book.java does not. You may want to fix that.

你的书。我认为xml有点不对。它包含一个嵌入的person元素,而这本书。java不。你可能想要解决这个问题。