I installed py4J using pip in my conda virtual environment in Python. I wrote a super simple example AdditionApplication.java for testing py4J , but it will not compile, i.e.
javac AdditionApplication.java
does not respond that GatewayServer is undefined.
I am well versed 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();
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; At the beginning of the file, I got another error:
package py4j does not exist
Update 2
pip install py4j left the jar file in <PATH_TO_CONDA_ENVIRONMENT>/share/py4j/py4j0.8.1.jar . I added it to my class path with:
javac -cp <PATH_TO_CONDA_ENVIRONMENT>/share/py4j/py4j0.8.1.jar AdditionApplication.java
and displays
AdditionApplication.class
How to 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 check this:
>>> from py4j.java_gateway import JavaGateway >>> gateway = JavaGateway()
java python py4j
Amelio vazquez-reina
source share