4.1. py4j.java_gateway — Py4J Main API

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, GatewayClient, java_import, get_field, and get_method. The other module members are documented to support the extension of Py4J.

4.1.1. JavaGateway

4.1.1.1. Examples

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 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(GatewayParameters(auto_field=True))
>>> example2 = java_gateway2.jvm.py4j.examples.ExampleWithField()
>>> example2.member
1
>>> get_method(example2,'member')()
u'Hello World'

4.1.2. GatewayParameters

4.1.3. CallbackServerParameters

4.1.4. GatewayClient

4.1.5. GatewayConnection

4.1.6. JVMView

4.1.7. JavaObject

TBD

4.1.8. JavaMember

TBD

4.1.9. JavaClass

TBD

4.1.10. JavaPackage

4.1.11. PythonProxyPool

4.1.12. CallbackServer

4.1.13. CallbackConnection

4.1.14. Py4J Functions

The following functions get be used to import packages or to get a particular field or method when fields and methods in a Java class have the same name:

Questions/Feedback?