CXF 创建Service报“请使用授权目录机制将 jaxb-api.jar 放在引导类加载器中”错误

详细错误如下:

Creating Service {http://server.hw.demo/}HelloWorld from class demo.hw.server.HelloWorld
Exception in thread "main" java.lang.LinkageError: 正在从引导类加载器加载 JAXB 2.1 API, 但此 RI (来自jar:file:/E:/jinwork/cxf/WebContent/WEB-INF/lib/jaxb-impl-2.2.6.jar!/com/sun/xml/bind/v2/model/impl/ModelBuilder.class) 需要 2.2 API。请使用授权目录机制将 jaxb-api.jar 放在引导类加载器中。(请参阅 http://java.sun.com/j2se/1.6.0/docs/guide/standards/)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.(ModelBuilder.java:178)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:455)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.(JAXBContextImpl.java:303)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.(JAXBContextImpl.java:142)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1174)
    at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:162)
    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:597)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:210)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:368)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
    at org.apache.cxf.common.jaxb.JAXBContextCache$2.run(JAXBContextCache.java:267)
    at org.apache.cxf.common.jaxb.JAXBContextCache$2.run(JAXBContextCache.java:265)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.apache.cxf.common.jaxb.JAXBContextCache.createContext(JAXBContextCache.java:265)
    at org.apache.cxf.common.jaxb.JAXBContextCache.getCachedContextAndSchemas(JAXBContextCache.java:172)
    at org.apache.cxf.jaxb.JAXBDataBinding.createJAXBContextAndSchemas(JAXBDataBinding.java:464)
    at org.apache.cxf.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:330)
    at org.apache.cxf.service.factory.AbstractServiceFactoryBean.initializeDataBindings(AbstractServiceFactoryBean.java:86)
    at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.buildServiceFromClass(ReflectionServiceFactoryBean.java:478)
    at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.buildServiceFromClass(JaxWsServiceFactoryBean.java:690)
    at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.initializeServiceModel(ReflectionServiceFactoryBean.java:540)
    at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.create(ReflectionServiceFactoryBean.java:252)
    at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.create(JaxWsServiceFactoryBean.java:205)
    at org.apache.cxf.jaxws.ServiceImpl.createPort(ServiceImpl.java:439)
    at org.apache.cxf.jaxws.ServiceImpl.getPort(ServiceImpl.java:312)
    at org.apache.cxf.jaxws.ServiceImpl.getPort(ServiceImpl.java:307)
    at javax.xml.ws.Service.getPort(Service.java:120)
    at demo.hw.client.Client.main(Client.java:54)

解决办法:

1.首先获取到endorsed目录。

public class MainClass {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(System.getProperty("java.endorsed.dirs"));
    }

}

2.找到相应位置,将apache-cxf-2.7.4\lib\endorsed下的jar文件放到对应目录下即可。如果没有endorsed目录,则手动建立该目录。

你可能感兴趣的:(Java)