解决maven无法编译通过sun内部类,报MojoFailureException异常

作者在项目本地测试正常,但在进行maven install打包时出现错误。查资料后发现有人认为是JDK版本问题,更换后无效。最终定位到内部类,了解到使用JDK内部类或接口时maven编译易报此异常。网上有两种解决方法,作者亲测添加maven - compiler - plugin插件并配置compilerArguments可行。

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

     这几天写项目一直都是在自己本地通过idea启动,然后本地测试,没有打包,也没有往服务器发布,并没有发现什么问题,一切正常。今天需要打包一下。忽然在maven install的过程中就出现了如下错误。

[WARNING] The requested profile "dev" could not be activated because it does not exist.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project lls-cloud-product-server: Compilation failure
[ERROR] ���һ��ѭ��: true
[ERROR] D:\develop\code\svnlz\lls\lls-cloud-product-server\src\main\java\com\lenovoedu\product\controller\ProductSolutionsController.java:[10,66] ����: �����com.sun.xml.internal.messaging.saaj.packaging.mime.internet������
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Process finished with exit code 1

查了好多资料。有的说是jdk版本不对,换个jdk,就好了。于是乎,就果断换了jdk,然并卵。

最后发现错误信息总是定位到下面这个类。

于是开始从这个类入手,了解到。这是个内部类,当maven项目里面有用到JDK内部的一些类或者接口的时候,用maven编译一般就会报这个异常。看来是找对了方向。

针对这个问题,网上有两种解决方法。

1.不使用这个类,而是用java或者javax中的类来替代。(这种方法我没有尝试)

2.第二种方法(亲测可行)

添加maven-compiler-plugin插件,并且配置compilerArguments

如:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <compilerArgs>
            <arg>-XDignore.symbol.file</arg>
        </compilerArgs>
        <fork>true</fork>
    </configuration>
</plugin>

另外可以看下这篇文章http://scmroad.com/topics/25883

pom.xml:<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>jdwlw</groupId> <artifactId>jdwlw</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>maven Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>com.sun.xml.messaging.saaj</groupId> <artifactId>saaj-impl</artifactId> <version>1.5.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.1.6.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.ksoap2/ksoap2 --> <dependency> <groupId>org.ksoap2</groupId> <artifactId>ksoap2</artifactId> <version>2.1.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.6.RELEASE</version> </dependency> <!-- <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.1.6.RELEASE</version> </dependency> --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.2.13.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc-portlet</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-websocket</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>quartz</groupId> <artifactId>quartz</artifactId> <version>1.5.2</version> </dependency> <dependency> <groupId>org.apache.ibatis</groupId> <artifactId>ibatis-sqlmap</artifactId> <version>2.3.4.726</version> </dependency> <!-- <dependency>--> <!-- <groupId>com.ibatis</groupId>--> <!-- <artifactId>ibatis2-sqlmap</artifactId>--> <!-- <version>2.1.7.597</version>--> <!-- </dependency>--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.6</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency> <dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.4.1</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.5.0-b01</version> </dependency> <!-- 激光消息推送Starts --> <dependency> <groupId>cn.jpush.api</groupId> <artifactId>jpush-client</artifactId> <version>3.1.3</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>17.0</version> </dependency> <dependency> <groupId>com.squareup.okhttp</groupId> <artifactId>mockwebserver</artifactId> <version>1.5.4</version> <scope>test</scope> </dependency> <!-- 激光消息推送END --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.11</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.0</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-net/commons-net --> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.3</version> </dependency> <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.ws.commons.axiom/axiom-api --> <dependency> <groupId>org.apache.ws.commons.axiom</groupId> <artifactId>axiom-api</artifactId> <version>1.2.13</version> </dependency> <!-- https://mvnrepository.com/artifact/dom4j/dom4j --> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.axis/axis --> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.xml.rpc/javax.xml.rpc-api --> <dependency> <groupId>javax.xml.rpc</groupId> <artifactId>javax.xml.rpc-api</artifactId> <version>1.1.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.axis2/axis2 --> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2</artifactId> <version>1.7.6</version> <type>pom</type> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.axis2/axis2-adb --> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-adb</artifactId> <version>1.7.6</version> </dependency> <!-- https://mvnrepository.com/artifact/soap/soap --> <dependency> <groupId>soap</groupId> <artifactId>soap</artifactId> <version>2.3</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient --> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.10.4</version> </dependency> <dependency> <groupId>org.eclipse.paho</groupId> <artifactId>org.eclipse.paho.client.mqttv3</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.2</version> </dependency> </dependencies> <build> <finalName>jdwlw</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>utf-8</encoding> <compilerArguments> <extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib</extdirs> </compilerArguments> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project> 强制启用 sun.misc来解决mvn package错:[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project jdwlw: Compilation failure: Compilation failure: [ERROR] /D:/biancheng/yf服务器代码/jdwlw/src/main/java/com/source/root/tools/util/DESUtil.java:[11,16] 找不到符号 [ERROR] 符号: 类 BASE64Decoder [ERROR] 位置: 程序包 sun.misc [ERROR] /D:/biancheng/yf服务器代码/jdwlw/src/main/java/com/source/root/tools/util/DESUtil.java:[12,16] 找不到符号 [ERROR] 符号: 类 BASE64Encoder [ERROR] 位置: 程序包 sun.misc [ERROR] /D:/biancheng/yf服务器代码/jdwlw/src/main/java/com/source/root/tools/util/DESUtil.java:[38,28] 找不到符号 [ERROR] 符号: 类 BASE64Encoder [ERROR] 位置: 类 com.source.root.tools.util.DESUtil [ERROR] /D:/biancheng/yf服务器代码/jdwlw/src/main/java/com/source/root/tools/util/DESUtil.java:[55,17] 找不到符号 [ERROR] 符号: 类 BASE64Decoder [ERROR] 位置: 类 com.source.root.tools.util.DESUtil [ERROR] /D:/biancheng/yf服务器代码/jdwlw/src/main/java/com/source/root/tools/util/DESUtil.java:[55,45] 找不到符号 [ERROR] 符号: 类 BASE64Decoder [ERROR] 位置: 类 com.source.root.tools.util.DESUtil [ERROR] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
最新发布
07-02
<think>用户的问题是关于如何强制启用sun.misc来解决Maven打包时的编译错误。错误信息显示在DESUtil.java文件中找不到BASE64Decoder和BASE64Encoder类,这些类属于sun.misc包。首先,我需要确认sun.misc包中的这些类是否仍然可用,或者是否已被弃用。我记得在Java8及更早版本中,sun.misc包中的BASE64Encoder和BASE64Decoder是存在的,但从Java9开始,这些内部API被模块化系统限制,不再默认可用。用户当前的Maven项目使用的是JDK1.8(因为pom.xml中配置了source和target为1.8),所以理论上这些类应该可用,但可能由于编译参数或模块设置的问题导致无法访问。接下来,我需要考虑可能的解决方案。用户提到要“强制启用sun.misc”,这可能涉及到在编译时添加特定的参数,比如--add-exports或--add-opens,以允许访问这些内部API。另外,检查Maven编译插件的配置是否正确,特别是是否指定了正确的JDK版本,并且是否传递了必要的编译器参数。查看用户的pom.xml,发现maven-compiler-plugin配置了source和target为1.8,但可能没有添加必要的参数来启用对sun.misc包的访问。因此,修改编译器插件的配置,添加<compilerArgs>来包含--add-exportsjava.base/sun.misc=ALL-UNNAMED等参数,可能会解决问题。此外,还需要确认项目中是否确实需要这些类,或者是否有更现代的替代方案,比如使用java.util.Base64(从Java8开始引入),但用户可能因为旧代码而无法立即替换。因此,临时解决方案是允许访问sun.misc包中的类。总结步骤:修改maven-compiler-plugin的配置,添加编译器参数以启用对sun.misc包的访问,确保使用正确的JDK版本,并检查项目依赖是否正确。</think>要解决因`sun.misc.BASE64Encoder/Decoder`缺失导致的编译错误,需在Maven编译插件中强制启用对`sun.misc`包的访问。修改`pom.xml`中`maven-compiler-plugin`的配置如下: ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>utf-8</encoding> <compilerArgs> <!-- 强制启用sun.misc包 --> <arg>--add-exports</arg> <arg>java.base/sun.misc=ALL-UNNAMED</arg> </compilerArgs> <compilerArguments> <extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib</extdirs> </compilerArguments> </configuration> </plugin> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值