Questions tagged [py4j]

Py4J enables Python programs to dynamically access arbitrary Java objects

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. Py4J also enables Java programs to call back Python objects. Py4J is distributed under the BSD license.

Here is a brief example of what you can do with Py4J. The following Python program creates a java.util.Random instance from a JVM and calls some of its methods. It also accesses a custom Java class, AdditionApplication to add the generated numbers.

 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

9
235 questions
0
votes
1 answer

Both ways, java <=> python, communication using py4j

I am using py4j for communication between python and java.I am able to call python method from java side. But from python I am not able to send any object or call java method. Here is the code i have tried. My java code: public interface IHello { …
naik3
  • 319
  • 2
  • 8
  • 22
0
votes
1 answer

py4j is opening too many threads

I am using py4j to communicate from Java to Python. I packed the Java code below into a Jar and I am running in with the command java -jar file.jar BUT looking into it I can see this command runs about 30 times in separate threads although I called…
Yael Green
  • 71
  • 7
0
votes
1 answer

Py4j launch_gateway not connecting properly

I am trying to use py4j to open up a gateway that I can use to pass objects from java into python. When I try to open a gateway with the py4j function launch_gateway it does not seem to properly connect to my Java class. However, when I launch my…
Grr
  • 15,553
  • 7
  • 65
  • 85
0
votes
2 answers

No module named py4j.protocol on Eclipse (PyDev)

I configured Eclipse in order to develop with Spark and Python. I configured : 1. PyDev with the Python interpreter 2. PyDev with the Spark Python sources 3. PyDev with the Spark Environment variables. This is my Libraries configuration : And…
Arij SEDIRI
  • 2,088
  • 7
  • 25
  • 43
0
votes
0 answers

apache-spark - Error when starting pyspark on windows

I'm trying to experiment with MLlib on windows with python. So it seems I need SPARK which in turn needs HADOOP. I've installed Anaconda2 which contains python 2.7, numpy, etc. I've been following this recipe which seems to me mostly getting me…
Simon
  • 151
  • 1
  • 7
0
votes
0 answers

IPython- Py4JJavaError:

I get a Py4JJavaError: when I try to create a data frame from rdd in pyspark. The problem is .createDataFrame() works in one ipython notebook and doesn't work in another. Strange. I have been trying to find out if there is synatx error I could nt…
Susie
  • 1
  • 3
0
votes
1 answer

Python byte array from jvm via py4j

I'm using py4j to send a byte array (Array[Byte]) from Scala to python. On the python side I wish to create a numpy array (preferably immutable) that is just a view of these bytes but interpreted as np.complex128. Disregarding byte order, the bytes…
John Pertoft
  • 1,045
  • 1
  • 9
  • 17
0
votes
1 answer

corenlp sentiment Java program via Py4j in Python, raises errors

I made a Java sentiment analysis program to be used in Python via Py4j. I can create the Java object in Python, but try to access the method, it gives java.lang.NullPointerException. Could you help please? Thanks. Java code: it compiles correctly,…
W.S.
  • 647
  • 1
  • 6
  • 19
0
votes
1 answer

Compile error in Java corenlp sentiment score program via py4j in Python

I mainly use Python and new to Java. However I am trying to write a Java program and make it work in Python via Py4j Python package. Following program is what I adapted from an example. I encountered a compile error. Could you shed some light? I am…
W.S.
  • 647
  • 1
  • 6
  • 19
0
votes
1 answer

PHP-Java-Bridge , usage of extend?

Java: package test; public class HelloWorld { public HelloWorld(String args){ } public void ppp(){ System.out.println("...."); }; public void set(HelloWorld hw){ hw.ppp(); } public static final String JAVABRIDGE_PORT = "28080"; static…
DunkOnly
  • 1,682
  • 4
  • 17
  • 39
0
votes
0 answers

Spark Streaming. Issues with Py4j: Error while obtaining a new communication channel

I am currently running a real time Spark Streaming job on a cluster with 50 nodes on Spark 1.3 and Python 2.7. The Spark streaming context reads from a directory in HDFS with a batch interval of 180 seconds. Below are the configuration for the Spark…
Nitin Singh
  • 76
  • 1
  • 1
  • 8
0
votes
1 answer

Py4j Exceptions when running application in a server

I have created an application using py4j that makes it possible to save data from python in to SQL database using a java application,everything works so fine when i run the JVM as an application and it actually saves the data. But when i run the…
raeX
  • 3,197
  • 2
  • 14
  • 21
0
votes
0 answers

Save new values of java object using Py4J

I'm just curious if there is any function in Py4j that enables saving new values in Java object. I mean, using get_field function I can easily get values of java object and with set_field, I can change that values, but I guess, only in Python. I…
Ktos
  • 47
  • 8
0
votes
1 answer

ImportError: No module named py4j.java_gateway

I am trying to test calling a java program from Python using py4j. I have installed the plugin in Eclipse and created pydev project named test. I am trying to execute the following piece of code I found on the py4j webpage: from py4j.java_gateway…
Abdul husein
  • 21
  • 1
  • 6
-1
votes
0 answers

Facing Error while Inserting Dataframe to PostgreSql using pyspark

Facing Error while Inserting Dataframe to PostgreSql using pyspark I am trying to insert a dataframe to my postgresql database using Pyspark but its giving Py4JJavaError . I have also installed java 8 , my python version is 3.11 and pyspark version…
Tushar
  • 1
  • 1
1 2 3
15
16