title: Running unit tests with Spark 3.3.0 on Java 17 fails with IllegalAccessError: class StorageUtils cannot access class sun.nio.ch.DirectBuffer
body: <p>According to the <a href="https://spark.apache.org/releases/spark-release-3-3-0.html" rel="nofollow noreferrer">release notes</a>, and specifically the ticket <em>Build and Run Spark on Java 17 (<a href="https://issues.apache.org/jira/browse/SPARK-33772" rel="nofollow noreferrer">SPARK-33772</a>)</em>, Spark now supports running on Java 17.</p>
  <p>However, using Java 17 (Temurin-17.0.3+7) with Maven (3.8.6) and maven-surefire-plugin (3.0.0-M7), when running a unit test that uses Spark (3.3.0) it fails with:</p>
  <blockquote>
  <p><code>java.lang.IllegalAccessError: class org.apache.spark.storage.StorageUtils$ (in unnamed module @0x1e7ba8d9) cannot access class sun.nio.ch.DirectBuffer (in module java.base) because module java.base does not export sun.nio.ch to unnamed module @0x1e7ba8d9</code></p>
  </blockquote>
  <p>The stack is:</p>
  <pre><code>java.lang.IllegalAccessError: class org.apache.spark.storage.StorageUtils$ (in unnamed module @0x1e7ba8d9) cannot access class sun.nio.ch.DirectBuffer (in module java.base) because module java.base does not export sun.nio.ch to unnamed module @0x1e7ba8d9
    at org.apache.spark.storage.StorageUtils$.&lt;init&gt;(StorageUtils.scala:213)
    at org.apache.spark.storage.StorageUtils$.&lt;clinit&gt;(StorageUtils.scala)
    at org.apache.spark.storage.BlockManagerMasterEndpoint.&lt;init&gt;(BlockManagerMasterEndpoint.scala:114)
    at org.apache.spark.SparkEnv$.$anonfun$create$9(SparkEnv.scala:353)
    at org.apache.spark.SparkEnv$.registerOrLookupEndpoint$1(SparkEnv.scala:290)
    at org.apache.spark.SparkEnv$.create(SparkEnv.scala:339)
    at org.apache.spark.SparkEnv$.createDriverEnv(SparkEnv.scala:194)
    at org.apache.spark.SparkContext.createSparkEnv(SparkContext.scala:279)
    at org.apache.spark.SparkContext.&lt;init&gt;(SparkContext.scala:464)
    at org.apache.spark.SparkContext$.getOrCreate(SparkContext.scala:2704)
    at org.apache.spark.sql.SparkSession$Builder.$anonfun$getOrCreate$2(SparkSession.scala:953)
    at scala.Option.getOrElse(Option.scala:189)
    at org.apache.spark.sql.SparkSession$Builder.getOrCreate(SparkSession.scala:947)
    [...]
  </code></pre>
  <p>The question <a href="https://stackoverflow.com/questions/72230174"><em>Java 17 solution for Spark - java.lang.NoClassDefFoundError: Could not initialize class org.apache.spark.storage.StorageUtils</em></a> was asked only 2 months ago, but this pre-dated the Spark 3.3.0 release, and thus predated official support for Java 17.</p>
  <p>Why can't I run my Spark 3.3.0 test with Java 17, and how can we fix it?</p>