6

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?

icedwater
  • 4,701
  • 3
  • 35
  • 50

3 Answers3

11

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.

e9t
  • 15,534
  • 5
  • 23
  • 25
5

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

if not jpype.isJVMStarted():
    jpype.startJVM(jvmPath, args)
Giacomo
  • 1,796
  • 1
  • 24
  • 35
1

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.

frage
  • 719
  • 7
  • 15
  • To revive this old answer, how did you implement this suggestion of Martin Taylor? I honestly do not understand what "*Then each API call just calls the NCh..*" would mean for me... – Adriaan Jul 26 '23 at 08:22
  • Not sure what you mean, it’s been a long time. From what I remember you have to start the jvm at the entry point of your app. – frage Jul 27 '23 at 01:15
  • I wanted to switch app within a jupyter notebook, without having to restart the kernel. – Adriaan Aug 02 '23 at 12:20
  • I did this in the function I wanted to use, just wrap it in the logic above to not restart. Just remember to close it when you app closes. Once jvm is started you can then import you Java module. – frage Aug 03 '23 at 01:25
  • I will read a bit on how Jpype opens and closes .jar files after the JVM has started. As I understood now: 1) the JVM is opened with a .jar, 2) after the operations are done it should be closed, 3–n) a new .jar can be loaded, used, and closed, n+1) JVM should be closed. – Adriaan Aug 03 '23 at 08:27