Search code examples
pythonanacondajpype

JPype won't compile properly when run the code twice


my code:

jarLocation = "C:/Users/LahiruGunawardhana/Desktop/New folder/info/infodynamics.jar"
# Start the JVM (add the "-Xmx" option with say 1024M if you get crashes due to not enough memory space)

jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=" + jarLocation,"-Xmx256m")

teCalcClass = jpype.JPackage("infodynamics.measures.discrete").TransferEntropyCalculatorDiscrete
teCalc = teCalcClass(2,1)
teCalc.initialise()

noOfEdges=0
for i in range(0,N-1):
  for j in range(i+1,N):
   # print(i,"-->",j)
    if(nodes_connectivity(j,i) | nodes_connectivity(i,j)):

     sss = [int(i) for i in SsourceArray]
     ddd = [int(i) for i in DdestArray]
     teCalc.addObservations(sss, ddd)
     e=teCalc.computeAverageLocalOfObservations()

jpype.shutdownJVM()

I'm getting values only once for this code. when i run this code a second time I'm getting an error saying:

_jpype.startup(jvm, tuple(args), True)

OSError: JVM is already started

Somehow, when i restart the python console for each step it gives values.


Solution

  • Check isJVMStarted() before startJVM() so it would be something like:

    if jpype.isJVMStarted():
        return
    
    jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=" + jarLocation,"-Xmx256m")
    

    So if you run twice and a JVM is running this code will avoid initializing it again