Using Py4J is usually as simple as creating a JavaGateway object:
java_gateway = JavaGateway()
# you are now connected to the JVM
# and you can call any method defined on the Java side.
You can still customize and extend Py4J in many ways (e.g., you can choose the port to which you want to connect), so here are the classes you are the most likely to interact with:
A JavaGateway is the main interaction point between a Python VM and a JVM.
Methods that are not defined by JavaGateway are always redirected to entry_point. For example, gateway.doThat() is equivalent to gateway.entry_point.doThat(). This is a trade-off between convenience and potential confusion.
Parameters: |
|
---|
Displays a help page about a class or an object.
Parameters: |
|
---|
Using the jvm property:
>>> gateway = JavaGateway()
>>> jvm = gateway.jvm
>>> l = jvm.java.util.ArrayList()
>>> l.append(10)
>>> l.append(1)
>>> jvm.java.util.Collections.sort(l)
>>> l
[1, 10]
>>> l.append(5)
>>> l.sort()
>>> l
[1, 5, 10]
Using non-default value of auto_start:
java_gateway = JavaGateway(auto_start=False)
java_gateway.comm_channel.start()
# ... do some work here
java_gateway.comm_channel.stop()
Using auto_field:
First we declare a class that has a field AND a method called member:
package py4j.examples;
public class ExampleWithField {
public int member = 1;
public String member() {
return "Hello World";
}
}
Then we play with the class using the two possible values of auto_field:
>>> java_gateway = JavaGateway() # auto_field = False
>>> example = java_gateway.jvm.py4j.examples.ExampleWithField()
>>> example.member()
u'Hello World'
>>> get_field(example,'member')
1
>>> java_gateway2 = JavaGateway(auto_field=True)
>>> example2 = java_gateway2.jvm.py4j.examples.ExampleWithField()
>>> example2.member
1
>>> get_method(example2,'member')()
u'Hello World'
Default communication channel (socket based) responsible for communicating with the Java Virtual Machine.
Parameters: |
|
---|
Closes the socket if auto_delete is True and the socket is opened.
This is an acceptable practice if you know that your Python VM implements garbage collection and closing sockets immediately is not a concern. Otherwise, it is always better (because it is predictable) to explicitly close the socket by calling CommChannel.close().
Sends a command to the JVM. This method is not intended to be called directly by Py4J users: it is usually called by JavaMember instances.
Parameter: | command – the string command to send to the JVM. The command must follow the Py4J protocol. |
---|---|
Return type: | the string answer received from the JVM. The answer follows the Py4J protocol. |
Exception thrown when a problem occurs with Py4J.
Parameter: | value – the error message |
---|
Represents a Java object from which you can call methods.
Parameters: |
|
---|
Maps a Python list to a Java list.
All operations possible on a Python list are implemented. For example, slicing (e.g., list[1:3]) will create a copy of the list on the JVM. Slicing is thus not equivalent to subList(), because a modification to a slice such as the addition of a new element will not affect the original list.
Retrieves the field named field_name from the java_object.
This function is useful when auto_field=false in a gateway or Java object.
Parameters: |
|
---|
Retrieves a reference to the method of an object.
This function is useful when auto_field=true and an instance field has the same name as a method. The full signature of the method is not required: it is determined when the method is called.
Parameters: |
|
---|
Replaces new line characters by a backslash followed by a n.
Backslashes are also escaped by another backslash.
Parameter: | original – the string to escape |
---|---|
Return type: | an escaped string |
Replaces escaped characters by unescaped characters.
For example, double backslashes are replaced by a single backslash.
Parameter: | escaped – the escaped string |
---|---|
Return type: | the original string |
Converts a Python object into a string representation respecting the Py4J protocol.
For example, the integer 1 is converted to u’i1’
Parameter: | parameter – the object to convert |
---|
Converts an answer received from the Java gateway into a Python object.
For example, string representation of integers are converted to Python integer, string representation of objects are converted to JavaObject instances, etc.
Parameters: |
|
---|