Fix: Apache Spark “Illegal Reflective Access” Error in Java 11

If you get the following warning when running Apache Spark in Java 11:

WARNING: Illegal reflective access by org.apache.spark.unsafe.Platform (file:spark-unsafe_2.13-3.2.1.jar) to constructor java.nio.DirectByteBuffer

You can fix it by adding this to your Java code before you create a Spark session:

import java.lang.annotation.Target;
...
Target.class.getModule().addOpens("java.nio", org.apache.spark.unsafe.Platform.class.getModule());