Created
August 26, 2013 08:04
-
-
Save mojaie/6339057 to your computer and use it in GitHub Desktop.
Sample of Py4J Python script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import atexit | |
import platform | |
import subprocess | |
import time | |
from py4j.java_gateway import JavaGateway | |
class JavaConnection(object): | |
JAR_PATH = "./jar/testentrypoint.jar" | |
ENTRY_POINT_CLASS = "TestEntryPoint" | |
def __init__(self): | |
self._gateway = None | |
self._process = None | |
cmd = ["java", "-jar", self.JAR_PATH, self.ENTRY_POINT_CLASS] | |
if platform.system() == "Windows": | |
cmd[0] = "javaw" | |
self._process = subprocess.Popen(cmd) | |
print "JVM started: " + str(self._process.pid) | |
atexit.register(self.kill) | |
time.sleep(1) # waiting for JVM start up | |
self._gateway = JavaGateway() | |
def getString(self): | |
return self._gateway.entry_point.getString() | |
def kill(self): | |
if self._gateway != None: | |
self._gateway.shutdown() | |
self._gateway = None | |
if self._process != None: | |
self._process.kill() | |
print "JVM killed: " + str(self._process.pid) | |
self._process = None | |
def main(): | |
jcon = JavaConnection() | |
print jcon.getString() | |
jcon.kill() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment