Jaxb编组与ClassNotFoundException失败

时间:2014-01-24 15:04:46

标签: java xml eclipse jaxb

我编译了我的xsd doc并开始为我的包装器编写junit测试。

当我打电话给marshal时,我得到下面的堆栈跟踪。没有找到日食课,我感到很惊讶。 (org.eclipse.persistence.jaxb.JAXBContextFactory)

我的课程中缺少什么?或者还有其他我想念的东西?

我正在运行eclipse indigo 3.7.2。

我的类路径包括以下jar: jaxb-api.jar,jaxb-impl.jar,jaxb-xjc.jar,jaxb1impl.jar,jsr173_1.0_api.jar,

我的编译器设置为java 6 compatability。

1 [main] WARN com.bellsouth.snt.cnmp.dao.csp.CspInputMarshaller  - Cannot marshal xml data
javax.xml.bind.JAXBException: Provider org.eclipse.persistence.jaxb.JAXBContextFactory not found
 - with linked exception:
[java.lang.ClassNotFoundException: org.eclipse.persistence.jaxb.JAXBContextFactory]
    at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
at javax.xml.bind.ContextFinder.find(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at com.bellsouth.snt.cnmp.dao.csp.CspInputMarshaller.jaxbMarshal(CspInputMarshaller.java:69)
at com.bellsouth.snt.cnmp.dao.csp.CspInputMarshaller.marshal(CspInputMarshaller.java:46)
at com.bellsouth.snt.cnmp.dao.csp.CspInputMarshallerTest.testGetUserRequest(CspInputMarshallerTest.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite.runTest(TestSuite.java:230)
at junit.framework.TestSuite.run(TestSuite.java:225)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: org.eclipse.persistence.jaxb.JAXBContextFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at javax.xml.bind.ContextFinder.safeLoadClass(Unknown Source)
... 26 more

更新:手动解决方法 缺少的jar文件是jaxb-impl.jar它在我的类路径中包含在ear项目中的jar中。如果我在默认类路径之前的类路径中添加jaxb-impl.jar。它运行良好。

在同一个工作区的其他项目中,我没有这个问题。

1 个答案:

答案 0 :(得分:1)

您可能需要以下依赖项,如果您使用的是Maven,请参考以下依赖项:

    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.moxy</artifactId>
        <version>2.6.8</version>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.2.11</version>
    </dependency>