Problem Description
This article uses jdk1.8.0_202, which is installed in the D disk, and the system environment variable path is configured.
D:\Java\jdk8\bin;D:\Java\jdk8\jre\bin;
The dependencies of calling webservice in this article are as follows:
<!-- 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>
Call method:
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(wsUrl);
Log error:
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)
Resolution process
First follow the popular solutions on the Internet, copy jdk/lib/tools.jar to jre/lib at the same level as jdk.
Note: To see the tools.jar, you must open the folder to hide the directory display.
Then send the request again, the error is as follows:
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)
Package cxf null pointer, I looked at my environment variable configuration, the configuration is jdk/bin and jdk/jre/bin respectively, then I directly copy tools. null pointer.
I saw someone on the Internet saying, and then I added this piece of code before dcf.createClient(wsUrl) initialized the Client:
// xxx是当前类的名字
Thread.currentThread().setContextClassLoader(xxx.class.getClassLoader());
I found that it was still a null pointer, so I changed it to this:
Thread.currentThread().setContextClassLoader(Client.class.getClassLoader());
Still not working.
solution
So I replaced it with jdk1.8.0_341 (click to use the quark network disk to download), and use the default installation path (C:\Program Files\Java, so there is no need to configure environment variables).
Then continue the above steps, copy tools.jar under jdk/lib to jdk/jre/lib and jre/lib at the same level of jdk respectively. Problem solved successfully.
NICE NICE NICE NICE !!!
Summary: Pay attention to version issues, and copy tools.jar to jre/lib.
- apache-cxf-3.2.XX start JDK minimum requirement is JDK1.8
- apache-cxf-3.1.XX still supports JDK1.7