问题描述
本文采用的是 jdk1.8.0_202,装在 D 盘中,配置了系统环境变量 path。
D:\Java\jdk8\bin;D:\Java\jdk8\jre\bin;
本文调用 webservice 的依赖如下:
<!-- webservice调用依赖cxf-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
<!-- webservice调用依赖axis-->
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-saaj</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.5</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
调用方法:
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(wsUrl);
日志报错:
java.lang.ClassNotFoundException: com/sun/tools/internal/xjc/api/XJC
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.apache.cxf.common.jaxb.JAXBUtils.createSchemaCompiler(JAXBUtils.java:711)
at org.apache.cxf.common.jaxb.JAXBUtils.createSchemaCompilerWithDefaultAllocator(JAXBUtils.java:725)
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createSchemaCompiler(DynamicClientFactory.java:423)
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:307)
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:241)
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:234)
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:189)
解决流程
先按照网络上热门的解决方案,将 jdk/lib/tools.jar 复制到 jdk 同级的 jre/lib 下面。
注意:要打开文件夹隐藏目录显示,才能看到 tools.jar 。
然后又发送请求,报错如下:
java.lang.NullPointerException: null
at org.apache.cxf.common.util.Compiler.useJava6Compiler(Compiler.java:187)
at org.apache.cxf.common.util.Compiler.compileFiles(Compiler.java:141)
at org.apache.cxf.common.util.Compiler.compileFiles(Compiler.java:136)
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.compileJavaSrc(DynamicClientFactory.java:611)
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:370)
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:241)
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:234)
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:189)
包 cxf 空指针,我看了一下我的环境变量配置,分别配置的是 jdk/bin 和 jdk/jre/bin ,那我直接把 tools.jar 拷进 jdk/jre/lib 试试,发现还是报 cxf 空指针。
我看到网上有人说,
然后,我就在 dcf.createClient(wsUrl) 初始化 Client 之前,加了这一段代码:
// xxx是当前类的名字
Thread.currentThread().setContextClassLoader(xxx.class.getClassLoader());
发现还是空指针,于是我改成了这个:
Thread.currentThread().setContextClassLoader(Client.class.getClassLoader());
仍然不行。
解决方案
于是我更换为 jdk1.8.0_341 (点击使用夸克网盘下载),并使用默认安装路径(C:\Program Files\Java,这样不用去配置环境变量)。
然后继续上述步骤,把 jdk/lib 下的 tools.jar 分别复制到 jdk/jre/lib 和 jdk 同级的 jre/lib 下面。成功解决问题。
NICE NICE NICE NICE !!!
小结: 注意版本问题,且要将 tools.jar 复制到 jre/lib 中。
- apache-cxf-3.2.XX 开始JDK最低要求要JDK1.8
- apache-cxf-3.1.XX还是支持JDK1.7的