【问题标题】:Call variables created in JVM after JVM is closed in JPypeJPype中关闭JVM后调用JVM中创建的变量
【发布时间】:2014-10-02 19:20:55
【问题描述】:

我在 JVM 中创建了一个 hashmap,想在 jpype.shutdownJVM 之后访问它,我该怎么做?

我知道 dict 在 python 中用作 hashmap,我试过了:

jpype.startJVM(jpype.getDefaultJVMPath(),"-Djava.class.path=%s" % 类路径) hashmap = jpype.java.util.HashMap() ...#插入对 jpype.shutdownJVM() ... hashmap["key"] 这不起作用。

谢谢。

【问题讨论】:

    标签: jpype


    【解决方案1】:

    密钥访问将映射到 java 调用。由于您已经关闭了 jvm,这将无法正常工作。

    您必须首先从 Java HashMap 创建一个新的 python 目录。确保这些值不是复杂的 Java 对象并且可以获取translated automatically

    import jpype
    jpype.startJVM(jpype.getDefaultJVMPath())
    hashmap = jpype.java.util.HashMap()
    # insert pairs
    hashmap.put("foo", "bar")
    # normally newmap = dict(hashmap) should work but jpype
    # doesn't seem to support this...
    newmap = {}
    for i in hashmap:
        # you might have to map more complex keys or values to python objects
        # before putting them into newmap
        newmap[i] = hashmap[i]
    jpype.shutdownJVM()
    print newmap["foo"]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-14
      • 2014-08-09
      • 2019-11-09
      • 2021-04-22
      • 1970-01-01
      • 2017-06-24
      相关资源
      最近更新 更多