15

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
Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564
  • 1
    I don't get it. I did exactly what you did here, but when I run: java -cp py4j0.10.8.1.jar AdditionApplication, I receive the following error:" Error: Could not find or load main class AdditionApplication". Any ideas? – Chiel Dec 21 '18 at 21:59
  • For Mac/LInux: java -cp /usr/local/share/py4j/py4j0.10.5.jar :. AdditionApplication​ For Windows: java -cp /usr/local/share/py4j/py4j0.10.5.jar AdditionApplication – Sreenu Yatam May 09 '21 at 14:01
  • 1
    @Chiel a bit late to the party but i had the same issue today. You also need to include the folder which contains `AdditionApplication.class` to the classpath. On windows this worked for me: `java -cp ".;C:\tools\miniconda3\envs\circe\share\py4j\py4j0.10.9.7.jar" AdditionApplication` – Frank Martin Mar 08 '23 at 10:50

1 Answers1

6

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

import py4j.GatewayServer;
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Thanks - I now get the error `package py4j does not exist`, but I installed py4j using `pip install py4j` from Python. – Amelio Vazquez-Reina Mar 13 '14 at 17:35
  • 3
    you need to include the `py4j0.x.jar` JAR file on your compile classpath, e.g. `javac -cp /usr/share/py4j/py4j0.x.jar:. AdditionApplication.java` – Reimeus Mar 13 '14 at 17:46
  • Thanks! The command now compiles to `AdditionApplication.class`. I imagine that in order to use it from Python it has to be running, since it's technically a server. How do I run it? – Amelio Vazquez-Reina Mar 13 '14 at 17:56
  • 1
    Never used p4j TBH - try `java -cp /usr/share/py4j/py4j0.x.jar:. AdditionApplication` – Reimeus Mar 13 '14 at 17:58
  • Thanks @Reimeus - When I run `javac ...` without the `.class` suffix, I got: `error: Class names, 'AdditionApplication' are only accepted if annotation processing is explicitly requested` – Amelio Vazquez-Reina Mar 13 '14 at 17:59
  • Thanks Reimeus - That worked! I understand these were basic Java questions - I appreciate it. – Amelio Vazquez-Reina Mar 13 '14 at 18:05
  • @user815423426 I tried doing the exact steps you did. I'm grateful you explained how you troubleshooted how to get your code working on Java and it did on mine too! ... but when I attempted to test it on the python interpreter, typing "from py4j.java_gateway import JavaGateway" made the python interpreter complain of an ImportError even when I added the py4j onto the PYTHONPATH (using import sys .. sys.path.append("..") help please? – compski Aug 16 '14 at 05:10
  • nvm i found out why all i needed to do was install setup.py from the interpreter like this : **python setup.py install --user** – compski Aug 16 '14 at 06:22