Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest example with py4J

Tags:

java

python

py4j

I installed py4J using pip on my conda virtual environment in Python. I wrote a super simple example AdditionApplication.java to test py4J, but it fails to compile, i.e.

javac AdditionApplication.java

fails complaining that GatewayServer is not defined.

I am knowledgeable in Python but unfortunately not in Java. What else do I need to provide?

public class AdditionApplication {

  public int addition(int first, int second) {
    return first + second;
  }

  public static void main(String[] args) {
    AdditionApplication app = new AdditionApplication();
    // app is now the gateway.entry_point
    GatewayServer server = new GatewayServer(app);
    server.start();
  }
}

In case it matters I have the following version of Java installed:

java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

Update 1

After I added: import py4j.GatewayServer; to the top of the file, I got a different error:

package py4j does not exist

Update 2

pip install py4j left a jar file under <PATH_TO_CONDA_ENVIRONMENT>/share/py4j/py4j0.8.1.jar. I have added it to my class path with:

javac -cp <PATH_TO_CONDA_ENVIRONMENT>/share/py4j/py4j0.8.1.jar AdditionApplication.java

and it output

AdditionApplication.class

How do I run it?

Final update and solution:

After applying the previous fixes, I finally run the code with:

java -cp <PATH_TO_CONDA_ENVIRONMENT>/share/py4j/py4j0.8.1.jar AdditionApplication 

the code runs in the background. To test it:

>>> from py4j.java_gateway import JavaGateway
>>> gateway = JavaGateway()                   # connect to the JVM
>>> random = gateway.jvm.java.util.Random()   # create a java.util.Random instance
>>> number1 = random.nextInt(10)              # call the Random.nextInt method
>>> number2 = random.nextInt(10)
>>> print(number1,number2)
(2, 7)
>>> addition_app = gateway.entry_point        # get the AdditionApplication instance
>>> addition_app.addition(number1,number2)    # call the addition method
like image 217
Amelio Vazquez-Reina Avatar asked Mar 13 '14 17:03

Amelio Vazquez-Reina


People also ask

What is the use of Py4J?

Py4J enables Python programs running in a Python interpreter to dynamically access Java objects in a Java Virtual Machine. Methods are called as if the Java objects resided in the Python interpreter and Java collections can be accessed through standard Python collection methods.

What is Py4J Java_gateway?

The py4j. java_gateway module defines most of the classes that are needed to use Py4J. Py4J users are expected to only use explicitly JavaGateway and optionally, GatewayParameters , CallbackServerParameters , java_import , get_field , get_method , launch_gateway , and is_instance_of .

Where is Py4J?

The Py4J Java library is located under py4j-java/py4j0. x. jar . Add this library to your classpath when using Py4J in a Java program.

What is Py4J PySpark?

Py4J is a popular library which is integrated within PySpark and allows python to dynamically interface with JVM objects. PySpark features quite a few libraries for writing efficient programs.


1 Answers

Import GatewayServer from the py4j package so that the unqualified class can be used in the application

import py4j.GatewayServer;
like image 68
Reimeus Avatar answered Oct 09 '22 08:10

Reimeus