java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > maven编译错误

Maven编译错误:程序包com.sun.*包不存在的三种解决方案

作者:super_贝塔

J2SE中的类大致可以划分为以下的各个包:java.*,javax.*,org.*,sun.*,本文文章主要介绍了maven编译错误:程序包com.sun.xml.internal.ws.spi不存在的解决方案,感兴趣的可以了解一下

maven 编译时报错:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:
程序包com.sun.…… 不存在
程序包com.sun.xml.internal.ws.spi不存在

官方解释

javac uses a special symbol table that does not include all Sun-proprietary classes. When javac is compiling code it doesn't link against rt.jar by default. Instead it uses special symbol file lib/ct.sym with class stubs.大意是:javac在编译时,并不引用 rt.jar,用的是一个特别的symbol table(lib/ct.sym),这个symbol table不包含所有的sun包的类。

具体原因

J2SE中的类大致可以划分为以下的各个包:java.*,javax.*,org.*,sun.*;除了“sun”包,其它各个包都是Java平台的标准实现,并且今后也将被继续支持。一般说来,“sun”之类的包并不包含在Java平台的标准中,它与操作系统相关,在不同的操作系统(如Solaris,Windows,Linux,Mac等等)中的实现也各不相同,并且可能随着J2SE版本不定期变化。因此,直接调用“sun”包的程序代码并不是100%的Java实现。也就是说:“sun.*”包并不是API公开接口的一部分,调用“sun”包的程序并不能确保工作在所有Java平台上,事实上,这样的程序并不能工作在今后的Java平台上。

解决方案一

<compilerArguments>  
    <verbose />  
    <bootclasspath>${JAVA_HOME}/jre/lib/rt.jar</bootclasspath>  
</compilerArguments>  

注意:${JAVA_HOME} 指的是你配置项目依赖的java路径(jdk版本)
1、如果使用的类,接口等在其他的jar里面(如tools.jar),则bootclasspath值需要配置成其他的jar
2、pom文件如果没有配置${JAVA_HOME} 导致会报错,也就是说这种方案行不通。

解决方案二 (亲测有效):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <encoding>UTF-8</encoding>
        <compilerArgs>
            <arg>-XDignore.symbol.file</arg>
        </compilerArgs>
        <fork>true</fork>
    </configuration>
</plugin>

切记:不要漏掉标签项 <fork>true</fork>

解决方案三

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.1</version>
  <configuration>
      <source>${java.version}</source>
      <target>${java.version}</target>
      <encoding>UTF-8</encoding>
      <compilerArguments>
          <bootclasspath>${java.home}/lib/rt.jar</bootclasspath>
      </compilerArguments>
  </configuration>
</plugin>

如果还是不行 调整配置如下:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <groupId>***(其他)***</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

将maven-compiler-plugin配置在后面,插件会覆盖springboot、其他集成的插件

到此这篇关于Maven编译错误:程序包com.sun.*包不存在的三种解决方案的文章就介绍到这了,更多相关maven编译错误内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文