user3376020
user3376020

Reputation:

Error in starting second JVM when one is already started

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?

Upvotes: 6

Views: 15137

Answers (3)

Giacomo
Giacomo

Reputation: 1955

I have solved it by adding these lines when defining the connection:

if not jpype.isJVMStarted():
    jpype.startJVM(jvmPath, args)

Upvotes: 5

frage
frage

Reputation: 743

This issue is not resolved by et9's answer above.

The problem is explained here.

Effectively you need to start/stop the JVM at the server/module level.

I have had success with multiple calls using this method in unit tests.

Upvotes: 1

e9t
e9t

Reputation: 16422

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.

Upvotes: 12

Related Questions