关于maven编译找不到com.sun的问题

2022-07-18 10:28:49

问题:

maven编译时报错:

[ERROR] D:\workspace\Test\Test.java:[3,25] 错误: 程序包com.sun.deploy.xml不存在

另外,java本地编译没有报错。

问题指向了Test.java类的三行import代码:

import com.sun.deploy.xml.XMLNode;
import com.sun.deploy.xml.XMLNodeBuilder;
import com.sun.javaws.jnl.XMLUtils;

前两个类来自jre/lib/下的deploy.jar

XMLUtils来自jre/lib/下的javaws.jar

问题的原因是maven编译时会忽略jre/lib下的jar包,导致报错。

解决方案:

在pom中添加

<compilerArguments>

    <extdirs>${java.home}\lib</extdirs>

</compilerArguments>

变成这样:

<plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-compiler-plugin</artifactId>

    <version>2.5.1</version>

    <configuration>

        <source>${java-version}</source>

        <target>${java-version}</target>

        <encoding>utf-8</encoding>

        <compilerArguments>

            <extdirs>${java.home}\lib</extdirs>

        </compilerArguments>

    </configuration>

</plugin>

或者这样:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.5.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <encoding>UTF-8</encoding>
        <compilerArguments>
            <verbose />
            <bootclasspath>${JAVA_HOME}/jre/lib/rt.jar${path.separator}${JAVA_HOME}/jre/lib/jce.jar</bootclasspath>
        </compilerArguments>
    </configuration>
</plugin>

然后maven编译就不报错了。

另外这个配置还能用来引入本地jar包的目录。

  • 作者:lkforce
  • 原文链接:https://blog.csdn.net/lkforce/article/details/108801297
    更新时间:2022-07-18 10:28:49