WebService接口调用报错:Unable to create schema compiler,com.sun.tools.internal.xjc.api.XJC

本文讲述了开发者在部署WeService接口时遇到的ClassNotFoundException,原因在于服务器上缺少JDK的tools.jar。通过引入三个特定的JAXB依赖,解决了因tools.jar缺失导致的JAXBException,详细介绍了问题原因和解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)问题描述

最近开发WeService接口的时候,遇到了一个报错,提示Caused by: java.lang.ClassNotFoundException: com.sun.tools.internal.xjc.api.XJC,这个问题,在我本地电脑上面代码可以正常运行,打包之后,将jar包发到服务器上面,就报错了。

根本原因是项目应用里面缺少tools.jar包,而tools.jar是jdk中提供的一个jar包,我本地电脑配置的环境是jdk的,所以可以正常运行,而服务器上面可能是jre环境,jre里面是没有tools.jar的,所以导致程序报错。

java.lang.IllegalStateException: Unable to create schema compiler
	at org.apache.cxf.common.jaxb.JAXBUtils.createSchemaCompilerWithDefaultAllocator(JAXBUtils.java:728)
	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)
	......
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)
	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
	at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
Caused by: javax.xml.bind.JAXBException: null
	at org.apache.cxf.common.jaxb.JAXBUtils.createSchemaCompiler(JAXBUtils.java:713)
	at org.apache.cxf.common.jaxb.JAXBUtils.createSchemaCompilerWithDefaultAllocator(JAXBUtils.java:720)
	... 22 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.sun.tools.internal.xjc.api.XJC
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:348)
	at org.apache.cxf.common.jaxb.JAXBUtils.createSchemaCompiler(JAXBUtils.java:706)
	... 23 common frames omitted

(2)解决方案

这次报错的根本原因就是项目中缺少jdk中的tools.jar包,解决办法就是想一个办法引入tools.jar包,但是需要注意的是,如果我们直接将tools.jar包放入打包之后的工程里面,此时仍然会报错,因为这种方式不生效。

最终的解决方案是,引入下面三个依赖,使用下面这三个依赖来替换tools.jar包的功能(相当于下面三个依赖可以实现和tools.jar相同的功能)。

<!-- 下面三个依赖,是解决缺少 jdk目录中的 tools.jar 包问题 start -->
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.2.1</version>
</dependency>
<dependency>
    <groupId>javax.xml</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.1</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-xjc</artifactId>
    <version>2.2.1.1</version>
</dependency>
<!-- 上面三个依赖,是解决缺少 jdk目录中的 tools.jar 包问题 end -->

到此,这个问题解决啦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.小朱同学

随心赞赏,助力前行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值