You need to enable JavaScript to run this app. Java11中AspectJ插件访问sun.security包报类型不可访问问题咨询
优惠活动
大模型
产品
解决方案
定价
更多

Java11中AspectJ插件访问sun.security包报类型不可访问问题咨询

解决方法

要解决AJC编译时的模块访问限制问题,你需要给aspectj-maven-plugin也传递和maven-compiler-plugin相同的模块导出参数,同时修正你当前配置里的错误依赖:

1. 新增编译参数配置

在aspectj-maven-plugin的全局configuration节点下添加<compilerArgs>配置,传入--add-exports参数:

<configuration>
  <showWeaveInfo>true</showWeaveInfo>
  <verbose>true</verbose>
  <source>11</source>
  <target>11</target>
  <complianceLevel>11</complianceLevel>
  <!-- 新增以下配置 -->
  <compilerArgs>
    <arg>--add-exports</arg>
    <arg>java.base/sun.security.validator=ALL-UNNAMED</arg>
  </compilerArgs>
</configuration>

2. 移除无效的tools.jar依赖

Java 11已经移除了tools.jar,你当前配置里的com.sun:tools依赖配置完全错误(systemPath指向了pom.xml文件),直接删除这个依赖节点即可:

<!-- 删除这段完全无效的配置
<dependency>
  <groupId>com.sun</groupId>
  <artifactId>tools</artifactId>
  <version>11</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/pom.xml</systemPath>
</dependency>
-->

修改后完整的aspectj-maven-plugin配置

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>aspectj-maven-plugin</artifactId>
  <version>1.7</version>
  <configuration>
    <showWeaveInfo>true</showWeaveInfo>
    <verbose>true</verbose>
    <source>11</source>
    <target>11</target>
    <complianceLevel>11</complianceLevel>
    <compilerArgs>
      <arg>--add-exports</arg>
      <arg>java.base/sun.security.validator=ALL-UNNAMED</arg>
    </compilerArgs>
  </configuration>
  <executions>
    <execution>
      <id>compile-with-aspectj</id>
      <phase>compile</phase>
      <goals>
        <goal>compile</goal>
      </goals>
      <configuration>
        <weaveDirectories>
          <weaveDirectory>${project.build.outputDirectory}/aspectJ-classes</weaveDirectory>
        </weaveDirectories>
      </configuration>
    </execution>
  </executions>
  <dependencies>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.9.6</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjtools</artifactId>
      <version>1.9.6</version>
    </dependency>
  </dependencies>
</plugin>

原理说明

Java 9及以上版本引入了模块化系统,默认禁止访问JDK内部的非公开API。AJC作为独立编译器,和javac遵循相同的模块访问规则,因此也需要显式配置--add-exports参数,将java.base模块下的内部包导出给未命名模块使用。

内容的提问来源于stack exchange,提问作者Piyush Narula

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 10:57:01