Error in starting second JVM when one is already started

user3376020 picture user3376020 · Aug 3, 2014 · Viewed 8.5k times · Source

I am developing a client-server software in which server is developed by python. I want to call a group of methods from a java program in python. All the java methods exists in one jar file. It means I do not need to load different jars.

For this purpose, I used jpype. For each request from client, I invoke a function of python which looks like this:

def test(self, userName, password):
    Classpath = "/home/DataSource/DMP.jar"
    jpype.startJVM(
        "/usr/local/java/jdk1.7.0_60/jre/lib/amd64/server/libjvm.so",
        "-ea",
        "-  Xmx512m",
        "-Djava.class.path=%s" % Classpath)

    NCh = jpype.JClass("Common.NChainInterface")
    n = NCh(self._DB_ipAddress, self._DB_Port, self._XML_SCHEMA_PATH, self._DSTDir)
    jpype.shutdownJVM()

For one function it works, but for the second call it cannot start jvm. I saw a lot of complain about it but I could not find any solution for that. I appreciate it if any body can help.

If jpype has problem in multiple starting jvm, is there any way to start and stop jvm once? The server is deployed on a Ubuntu virtual machine but I do not have enough knowledge to write for example, a script for this purpose. Could you please provide a link, or an example?

Answer

e9t picture e9t · Aug 26, 2014

Check isJVMStarted() before startJVM().
If JVM is running, it will return True, otherwise False.

def init_jvm(jvmpath=None):
    if jpype.isJVMStarted():
        return
    jpype.startJVM(jpype.getDefaultJVMPath())

For a real example, see here.