You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache Spark 3.3.0在Java 17运行时报错无法访问sun.nio.ch.DirectBuffer

解决Spark 3.3.0 + Java 17运行时IllegalAccessError问题

这个错误源于Java 17的模块化系统默认禁止访问sun.nio.ch.DirectBuffer这类内部API,而Spark 3.3.0的StorageUtils类需要调用该API,因此必须通过添加JVM参数来授权访问。

核心解决方案:添加JVM启动参数

给JVM添加以下参数,允许Spark相关模块访问Java内部API:

--add-opens java.base/sun.nio.ch=ALL-UNNAMED
--add-opens java.base/java.nio=ALL-UNNAMED
--add-opens java.base/java.lang=ALL-UNNAMED

不同场景的配置方式

1. IDE(IntelliJ/Eclipse)中运行

  • 打开运行配置(Run Configuration)
  • 在「VM options」(虚拟机参数)栏粘贴上述参数
  • 保存后重新运行程序

2. Maven运行(通过exec:java插件)

在pom.xml的exec插件配置中添加jvmArguments:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <goals>
                <goal>java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <mainClass>你的主类全路径</mainClass>
        <jvmArguments>
            --add-opens java.base/sun.nio.ch=ALL-UNNAMED
            --add-opens java.base/java.nio=ALL-UNNAMED
            --add-opens java.base/java.lang=ALL-UNNAMED
        </jvmArguments>
    </configuration>
</plugin>

执行mvn exec:java即可运行程序

3. 打包成jar后运行

用java命令启动时直接带上参数:

java --add-opens java.base/sun.nio.ch=ALL-UNNAMED --add-opens java.base/java.nio=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED -jar 你的jar包名称.jar

补充说明

Spark 3.3.0对Java 17的支持需要这些额外参数来兼容模块化限制,后续Spark版本(如3.4+)已优化了内部API调用逻辑,无需手动添加此类参数,但3.3.0版本必须通过该方式解决。

内容的提问来源于stack exchange,提问作者Garret Wilson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 17:36:11