Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

Spark tests on Java 17 stop because sun.nio.ch is not exported

Your error

class org.apache.spark.storage.StorageUtils$ (in unnamed module @0x1aa7ecca) cannot access class sun.nio.ch.DirectBuffer (in module java.base) because module java.base does not export sun.nio.ch to unnamed module @0x1aa7ecca1 more line it can show

Fix it fast

Most common cause, 1 of 3

The test or IDE JVM starts without Spark's --add-opens list

Give the test JVM the options from Spark's own list. sun.nio.ch clears this message; the other entries come from the same Spark list and open packages Spark reaches into elsewhere.

Fix
<!-- pom.xml, maven-surefire-plugin -->
<configuration>
  <argLine>--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 --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED</argLine>
</configuration>

// build.gradle
test {
    jvmArgs('--add-opens=java.base/sun.nio.ch=ALL-UNNAMED', '--add-opens=java.base/java.nio=ALL-UNNAMED')
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

StorageUtils$ cannot access class sun.nio.ch.DirectBuffer appears when Spark 3.x runs on Java 17 in a JVM that spark-submit did not start: Maven, Gradle or sbt tests, or an IDE run. spark-submit adds --add-opens=java.base/sun.nio.ch=ALL-UNNAMED by itself. Everywhere else, add that flag to the arguments of the JVM that creates the SparkSession.

  • Applies to Apache Spark 3.5.9 on Java 17, Maven Surefire 3.6.1, Gradle 9.9.2, sbt 1.13
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Print the Java version, then search the build files for JVM arguments. No argLine, jvmArgs or javaOptions with sun.nio.ch means cause 1. Flags that exist next to forkCount 0, a JaCoCo plugin or an sbt build without forking mean cause 2. A spark-core version below 3.3 means cause 3.

Run this first
java -version
grep -nE "argLine|forkCount|jacoco|jvmArgs|javaOptions|fork|spark-core" pom.xml build.gradle* build.sbt 2>/dev/null

Then find the line you see in this list and open its fix:

  1. 1The test or IDE JVM starts without Spark's --add-opens listThe same job works through spark-submit or spark-shell, and the build files hold no flag for sun.nio.ch
  2. 2The flags exist, but not in the JVM that actually runs the testsargLine, javaOptions or spark.driver.extraJavaOptions is already set and the error is unchanged
  3. 3The Spark version is older than 3.3 and has no Java 17 supportspark-core or pyspark is 3.2.x or lower, and more flags only move the failure elsewhere

What does the error look like?

  • class org.apache.spark.storage.StorageUtils$ (in unnamed module @0x1aa7ecca) cannot access class sun.nio.ch.DirectBuffer (in module java.base) because module java.base does not export sun.nio.ch to unnamed module @0x1aa7ecca
  • An exception or error caused a run to abort: class org.apache.spark.storage.StorageUtils$

Cause 1 of 3

The test or IDE JVM starts without Spark's --add-opens list

You seeThe same job works through spark-submit or spark-shell, and the build files hold no flag for sun.nio.ch

Check

The access error for StorageUtils$ and sun.nio.ch.DirectBuffer means Spark 3.5 code touched a JDK-internal class that Java 17 keeps closed. Spark's launcher class JavaModuleOptions (added in 3.3.0) holds the --add-opens options Spark needs, and SparkSubmitCommandBuilder appends them to every submit command. A test runner or IDE launches its own JVM and gets none of them.

Check
grep -n "sun.nio.ch" pom.xml build.gradle* build.sbt 2>/dev/null || echo "no module flag found"

Fix

Give the test JVM the options from Spark's own list. sun.nio.ch clears this message; the other entries come from the same Spark list and open packages Spark reaches into elsewhere. Use the Maven form or the Gradle form, whichever builds your project.

Fix
<!-- pom.xml, maven-surefire-plugin -->
<configuration>
  <argLine>--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 --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED</argLine>
</configuration>

// build.gradle
test {
    jvmArgs('--add-opens=java.base/sun.nio.ch=ALL-UNNAMED', '--add-opens=java.base/java.nio=ALL-UNNAMED')
}

Verify

The suite gets past SparkSession creation and the module access error no longer shows in the test report.

Verify
mvn test

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

The flags exist, but not in the JVM that actually runs the tests

You seeargLine, javaOptions or spark.driver.extraJavaOptions is already set and the error is unchanged

Check

Surefire documents argLine as effective only for forked executions, so forkCount set to 0 runs the tests inside Maven's JVM without it. JaCoCo's prepare-agent goal writes its own argLine property, which a plain argLine of yours can override. sbt hands javaOptions only to a forked JVM. Putting spark.driver.extraJavaOptions on a SparkConf in client mode is too late, because the driver JVM is already running.

Check
grep -nE "forkCount|@\{argLine\}|jacoco|Test / fork" pom.xml build.sbt 2>/dev/null

Fix

Keep Surefire forking (forkCount 1 or more) and start argLine with @{argLine} so JaCoCo's agent and your flags both survive. In sbt, fork the tests and attach the options to the Test scope. For spark-submit runs, pass driver options on the command line instead of in code.

Fix
<!-- pom.xml -->
<argLine>@{argLine} --add-opens=java.base/sun.nio.ch=ALL-UNNAMED</argLine>

// build.sbt
Test / fork := true
Test / javaOptions += "--add-opens=java.base/sun.nio.ch=ALL-UNNAMED"

Verify

The sbt or Maven test run finishes green and the coverage report is still written.

Verify
sbt test

Confirmed by maven.apache.org

Did this fix it?

Cause 3 of 3

The Spark version is older than 3.3 and has no Java 17 support

You seespark-core or pyspark is 3.2.x or lower, and more flags only move the failure elsewhere

Check

Apache Spark tracked Java 17 support in SPARK-33772, fixed in version 3.3.0; before that, Spark supported Java 8 and 11. The Spark 3.5.9 overview lists Java 8, 11 and 17. On an older Spark line no flag set makes Java 17 a supported runtime.

Check
grep -n -A1 "<artifactId>spark-core" pom.xml

Fix

Upgrade to Spark 3.5.9, or to 4.3.0, which runs on Java 17, 21 and 25 and is built only for Scala 2.13. If the upgrade has to wait, run the old Spark on Java 11, which it supports.

Fix
<dependency>
  <groupId>org.apache.spark</groupId>
  <artifactId>spark-core_2.13</artifactId>
  <version>3.5.9</version>
</dependency>

Verify

The pom now names 3.5.9 and the suite runs on Java 17 once the flags from cause 1 are in place.

Verify
mvn -q test

Confirmed by issues.apache.org

Did this fix it?

How do I stop it coming back?

  • Copy the whole JavaModuleOptions list from the Spark tag you depend on into one shared argLine property, and review it when you bump Spark.
  • Build argLine as @{argLine} plus your flags from the start, so adding JaCoCo later cannot drop them.
  • Pin the CI test job to the Java major version used in production; a missing module flag then breaks the pipeline, not a release.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: QistLock, Nuclear Home Video.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.