Fix the PySpark Java gateway error on Windows and WSL
If Spark fails at getOrCreate() with [JAVA_GATEWAY_EXITED] Java gateway process exited before sending its port number, start with the Java error printed above it. The final Python exception tells you that startup failed. The earlier message usually explains why.
Check Java from the terminal that runs your script
In the same VS Code terminal, run these commands. Save the output before changing anything.
python -c "import sys, pyspark; print(sys.executable); print(pyspark.__version__)" java -version
If the import fails, follow the missing PySpark module guide first. If java -version fails, install a compatible JDK and reopen the terminal. These examples use PySpark 4.2.0 with Python 3.13 and Java 21. Match the requirements of the Spark version you installed.
When Java works but Spark still fails
Inspect JAVA_HOME. Spark can use this variable even when a different, working Java is on your PATH. An old JDK directory can survive a Java upgrade.
$env:JAVA_HOME Get-Command java | Select-Object Source Test-Path "$env:JAVA_HOME\bin\java.exe"
If the last command returns False, the configured JDK directory is missing or incorrect. On the Windows machine used to check this tutorial, PATH found a newer Java 21 installation while JAVA_HOME still named an older directory. Spark printed The system cannot find the path specified. A successful java -version alone missed that mismatch.
Set JAVA_HOME to the root of the installed JDK for this terminal session. Replace the example path with the directory on your machine, and check the executable directly.
$env:JAVA_HOME = "C:\Program Files\Eclipse Adoptium\jdk-21.0.13.9-hotspot" & "$env:JAVA_HOME\bin\java.exe" -version python setup_check.py
This assignment affects this PowerShell session and the processes it starts. Once the check succeeds, update a stale user JAVA_HOME through Windows Environment Variables and restart VS Code so new processes inherit it.
Check WSL inside WSL
Open the project in a WSL window and check the Linux Java installation. A JDK installed on Windows does not supply a Linux Java executable.
which python3 which java printf "%s\n" "$JAVA_HOME" java -version
If JAVA_HOME contains a Windows path or a removed Linux directory, correct it to your installed Linux JDK. When Java is correctly available on PATH, you can test a single command without an inherited JAVA_HOME using env -u JAVA_HOME .venv/bin/python setup_check.py. Keep the project under a path such as ~/code/pyspark-check.
Read the earlier error before trying another fix
- UnsupportedClassVersionError means the JVM is too old for a class it is loading. Check the Spark version's Java requirement and the JVM selected through JAVA_HOME.
- Could not find or load main class can indicate a conflicting manual Spark installation. Inspect SPARK_HOME and any custom classpath before changing them.
- BindException points to an address or port problem. Diagnose the address in that message. For a strictly local test, setting
SPARK_LOCAL_IP=127.0.0.1in the launching process can help isolate hostname resolution.
Verify the fix with a small job
Download the free example below and run setup_check.py. It starts a local session, writes two rows to Parquet and reads them back. A successful run prints Rows written: 2. If startup now succeeds but the write fails, troubleshoot the new filesystem error separately. The installation guide covers the full setup.
Reference requirements against the Apache PySpark 4.2 installation documentation.