Open In App

Integrating Java with Python

Last Updated : 01 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads