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 showFix it fast
Most common cause, 1 of 3The 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.
<!-- 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')
}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.
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.
java -version
grep -nE "argLine|forkCount|jacoco|jvmArgs|javaOptions|fork|spark-core" pom.xml build.gradle* build.sbt 2>/dev/nullThen find the line you see in this list and open its fix:
- 1The test or IDE JVM starts without Spark's --add-opens list
The same job works through spark-submit or spark-shell, and the build files hold no flag for sun.nio.ch - 2The flags exist, but not in the JVM that actually runs the tests
argLine, javaOptions or spark.driver.extraJavaOptions is already set and the error is unchanged - 3The Spark version is older than 3.3 and has no Java 17 support
spark-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 @0x1aa7eccaAn 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.
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.
<!-- 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.
mvn testConfirmed by raw.githubusercontent.com
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.
grep -nE "forkCount|@\{argLine\}|jacoco|Test / fork" pom.xml build.sbt 2>/dev/nullFix
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.
<!-- 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.
sbt testConfirmed by maven.apache.org
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.
grep -n -A1 "<artifactId>spark-core" pom.xmlFix
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.
<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.
mvn -q testConfirmed by issues.apache.org
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.