两次运行代码时,JPype无法正确编译

时间:2018-10-04 10:16:09

标签: python anaconda jpype

我的代码:

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()

此代码仅获取一次值。当我第二次运行此代码时,出现错误消息:

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

OSError: JVM is already started

不知何故,当我为每个步骤重新启动python控制台时,它都会给出值。

1 个答案:

答案 0 :(得分:0)

startJVM()之前检查 isJVMStarted(),这样就可以了:

case 1

因此,如果您运行两次并且JVM正在运行,则此代码将避免再次对其进行初始化