Integrating Java with Python
Last Updated :
28 Apr, 2025
While programming in a language, a developer may feel the need to use some functionality that may have better support in another language. For example, suppose, an application has already been developed in Java, and we wish to use it in Python code. To invoke an existing Java application in Python, we need a bridge between Python and Java. Packages like Py4j, Pyjnius, Jpype, javabridge, and JCC help invoke Java programs from Python. Also, since Java provides a wide variety of collections, we can directly use them in a Python program by including their java packages in the program. We will make use of py4j for invoking Java functionality from Python. It can be installed by executing the following command from the command line:
pip install py4j
After this, we need to improve the IDE in which we will be writing our code. For example, if we are writing our Java program in Eclipse, we need to include the jar file of py4j in the Java project. It is important to point out that py4j supports Java version 6 and above only. Let us examine a simple Java program HelloClass.java that has a method Message which returns Hello.
To make the GFG accessible in a Python program, two things are required. First, the Python program should be able to access the Java Virtual Machine (JVM) running the Java program. This requires an instance of the GatewayServer class available in the library py4j that makes this communication possible. Secondly, an entry point should be mentioned in the call to the constructor of the class GatewayServer. This entry point can be any object we wish to deal with in a Python program. In our case, an object of GFG serves as an entry point (line 10). Here it is important to mention that a Python program will be able to use a Java program only if it (a Java program) is ready to accept incoming requests. Therefore, in line 11, we start the gateway using the method start, Also, for communication to take place, the Java program must be compiled and executed before the Python program.
Java
/*package whatever //do not write package name here */
import java.io.*;
import py4j.GatewayServer;
class GFG {
public String Message() { return "Hello"; }
public static void main(String[] args)
{
GatewayServer g = new GatewayServer(new GFG());
g.start();
System.out.println("Gateway Server Started");
}
}
The script hello.py uses the Java program that we discussed above. For this purpose, we need to create an instance of the class JavaGateway available in the module py4j.java_gateway. The first two lines of the Python script hello achieve this. When we invoke JavaGateway (line 2), Python tries to connect with the JVM of the Java program already running and returns an instance. We can access the entry point object using the member entry_point (line 3). This object now enables us to use Java functionality in the {ython program (line 4). However, if no JVM is waiting for a connection, an error message Py4JNetworkError is displayed.
Python3
from py4j.java_gateway import JavaGateway
gateway = JavaGateway()
msgObjectFromJavaApp = gateway.entry_point
print(msgObjectFromJavaApp.Message())
On running the script hello, Python outputs Hello. If the error message address already in use appears while running the Java program, we need to explicitly specify a new port number (between 1025 and 65535) in each of the Java and Python programs. For example, in the Java program GFG, we may replace line 10 by:
GatewayServer g = new GatewayServer(new GFG(), 25539);
and in the Python script hello we replace line 2 by:
gateway = JavaGateway(gateway_parameters = GatewayParameters(port(25539))
For setting the gateway_parameters explicitly, we need to import GatewayParameters along with the JavaGateway. Since we need to compile and execute the Java program first and then the Python program, we create a batch file (say, helloapp.bat) comprising these two tasks one after the other since compiling and running an eclipse program from the command line is a more complex job, we can convert the entire project to an executable jar file which can be used anywhere.
Similar Reads
Python 101 Welcome to "Python 101," your comprehensive guide to understanding and mastering the fundamentals of Python programming. Python is a versatile and powerful high-level programming language that has gained immense popularity due to its simplicity and readability. Whether you're an aspiring programmer,
13 min read
Best way to learn python Python is a versatile and beginner-friendly programming language that has become immensely popular for its readability and wide range of applications. Whether you're aiming to start a career in programming or just want to expand your skill set, learning Python is a valuable investment of your time.
11 min read
Python Automation Tutorial: Beginner to Advanced Python is a very powerful programming language and it's expanding quickly because of its ease of use and straightforward syntax. In this Python Automation Tutorial, we will explore various techniques and libraries in Python to automate repetitive tasks. Automation can save you time and reduce errors
10 min read
Introduction to Python for Absolute Beginners Are you a beginner planning to start your career in the competitive world of Programming? Looking resources for Python as an Absolute Beginner? You are at the perfect place. This Python for Beginners page revolves around Step by Step tutorial for learning Python Programming language from very basics
6 min read
Python â The new generation Language INTRODUCTION: Python is a widely-used, high-level programming language known for its simplicity, readability, and versatility. It is often used in scientific computing, data analysis, artificial intelligence, and web development, among other fields. Python's popularity has been growing rapidly in re
6 min read