Below is the complete, final README.md content.
You can copy everything in one go and paste it directly into your README.md file on GitHub.
# Simple Py4J Addition Example
This project demonstrates a **basic Py4J use case** where a **Python program calls a Java method** to perform a simple addition.
It is designed for beginners to clearly understand **how Python and Java communicate** using Py4J.
---
## 📌 What is Py4J?
**Py4J** is a bridge that allows **Python applications to call Java objects and methods** that are running inside a **separate Java Virtual Machine (JVM)**.
Py4J is widely used in real-world systems such as **PySpark**, where Python code controls a JVM-based computation engine.
---
## 🎯 Project Use Case
- Java acts as a **computation engine**
- Python acts as a **controller**
- Python sends two numbers to Java
- Java performs the addition and returns the result
This mirrors real-world scenarios where Python orchestrates powerful Java-based systems.
---
## 📁 Project Structure
py4j-demo/ ├── SimpleApp.java # Java GatewayServer ├── client.py # Python client ├── py4j-0.10.9.9.jar # Py4J Java library └── README.md
---
## 🧩 Requirements
- **Java JDK 8 or higher**
- **Python 3.8 or higher**
- Internet connection (to download the Py4J JAR if it is not already included)
---
## 🔽 Downloading the Py4J JAR File
Java requires the **Py4J JAR file** to start the `GatewayServer`.
You can obtain the JAR using **either of the following methods**.
---
### ✅ Method 1: Download from Maven Central (Recommended)
1. Open the following URL in your browser:
https://repo1.maven.org/maven2/net/sf/py4j/py4j/
2. Click on the version directory (recommended: `0.10.9.9`)
3. Download the file:
py4j-0.10.9.9.jar
4. Place the JAR file in your project directory:
py4j-demo/
✔ This is the **official and most reliable source** for the Py4J JAR.
---
### ✅ Method 2: Manual Download (Normal Way)
1. Open the Py4J website:
2. Navigate to the download section
3. Download the binary distribution
4. Extract the ZIP file
5. Locate the JAR file inside the extracted folder:
py4j-java/py4j-0.10.9.9.jar
6. Copy the JAR file into your project directory
---
## ☕ Java Program (SimpleApp.java)
The Java program:
- Starts a `GatewayServer`
- Exposes an `add` method to Python
### Compile Java
Open a terminal in the project directory and run:
```bat
javac -cp .;py4j-0.10.9.9.jar SimpleApp.java
java -cp .;py4j-0.10.9.9.jar SimpleAppExpected output:
Gateway Server Started
The Python program:
- Connects to the Java GatewayServer
- Calls the Java
addmethod - Prints the result returned by Java
pip install py4jOpen a new terminal in the same project directory and run:
python client.pyExpected output:
Addition: 30
- Start the Java program first
- Then run the Python program
Python cannot connect unless the Java GatewayServer is already running.
- Java acts as a server
- Python acts as a client
- Communication happens using a TCP socket connection
- Java does not understand Python packages
- The JAR contains the Java-side implementation of Py4J
- Without the JAR, the
GatewayServerclass will not exist
Python (client.py)
|
| TCP socket (Py4J)
|
Java (SimpleApp.java)
- PySpark (Python ↔ JVM communication)
- Big data processing engines
- Machine learning pipelines
- Python orchestration of Java-based systems