Mac环境下Jupyter Notebook启动PySpark Session遇Py4JJavaError问题求助
Fixing PySpark
NoClassDefFoundError on Mac Hey there! I’ve run into this exact issue before on Mac, so let’s break down what’s going wrong and how to fix it.
What’s Causing the Error?
The core problem is a version mismatch between your Java installation and Spark. That error about sun.nio.ch.DirectBuffer being inaccessible happens because:
- Older Spark versions (pre-3.0) aren’t compatible with Java 11 or newer. Java 11 introduced a module system that restricts access to internal classes like
sun.nio.ch.DirectBuffer, which Spark used to rely on. - It sounds like you installed a newer Java version (probably 11+), but your Spark installation is too old to work with it.
Solutions to Try
Option 1: Downgrade Java to Version 8 (Most Reliable for Older Spark)
Spark 2.x and older work best with Java 8. Here’s how to set this up on Mac:
- Install Java 8 using Homebrew:
brew install openjdk@8 - Update your
JAVA_HOMEenvironment variable to point to Java 8.- If you use Zsh, edit
~/.zshrc:echo 'export JAVA_HOME=/usr/local/opt/openjdk@8/libexec/openjdk.jdk/Contents/Home' >> ~/.zshrc source ~/.zshrc - If you use Bash, edit
~/.bash_profileinstead:echo 'export JAVA_HOME=/usr/local/opt/openjdk@8/libexec/openjdk.jdk/Contents/Home' >> ~/.bash_profile source ~/.bash_profile
- If you use Zsh, edit
- Verify the change by running:
You should see output indicating Java 8 (likejava -versionopenjdk version "1.8.0_XXX").
Option 2: Upgrade Spark to Version 3.0+ (If You Want to Keep Newer Java)
Spark 3.0 and later fixed this compatibility issue with Java 11+. To upgrade:
- Install the latest Spark via Homebrew:
brew install apache-spark - Make sure your
SPARK_HOMEvariable is set correctly (Homebrew usually sets this automatically, but you can confirm by runningecho $SPARK_HOME). - If you’re using PySpark in Jupyter, ensure it’s pointing to the new Spark installation. You can set this temporarily in your notebook before importing PySpark:
Replaceimport os os.environ['SPARK_HOME'] = '/usr/local/Cellar/apache-spark/<your-spark-version>/libexec'<your-spark-version>with the actual version number (like3.5.0).
Verify Your Fix
Once you’ve made the change:
- Test Spark in your terminal first by running
spark-shell— if it starts without errors, you’re good to go. - Go back to your Jupyter Notebook and re-run your Spark Session code:
import pyspark from pyspark.sql import SparkSession spark=SparkSession.builder.appName('test').getOrCreate()
内容的提问来源于stack exchange,提问作者IshN
相关产品推荐
相关产品推荐

