Skip to content

DevAta515/first_py2j

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

https://www.py4j.org


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

Run Java

java -cp .;py4j-0.10.9.9.jar SimpleApp

Expected output:

Gateway Server Started

⚠️ Keep this terminal running while executing the Python program.


🐍 Python Program (client.py)

The Python program:

  • Connects to the Java GatewayServer
  • Calls the Java add method
  • Prints the result returned by Java

Install Py4J (Python side)

pip install py4j

Run Python

Open a new terminal in the same project directory and run:

python client.py

Expected output:

Addition: 30

🔁 Execution Order (Very Important)

  1. Start the Java program first
  2. Then run the Python program

Python cannot connect unless the Java GatewayServer is already running.


🔍 Key Concepts Explained

Why Java must be running

  • Java acts as a server
  • Python acts as a client
  • Communication happens using a TCP socket connection

Why the JAR file is required

  • Java does not understand Python packages
  • The JAR contains the Java-side implementation of Py4J
  • Without the JAR, the GatewayServer class will not exist

🧠 Mental Model

Python (client.py)
        |
        |  TCP socket (Py4J)
        |
Java (SimpleApp.java)

🌍 Real-World Usage of Py4J

  • PySpark (Python ↔ JVM communication)
  • Big data processing engines
  • Machine learning pipelines
  • Python orchestration of Java-based systems

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors