java.lang.ClassCastException while using PythonInterpreter from Java

可紊 提交于 2019-12-22 06:17:10

问题


I am trying out to invoke python function passing it HashMap from within java (groovy). The python function squares every value in input map and returns Dictionary of squares with same keys.

JythonTest.groovy

import org.python.util.PythonInterpreter
import org.python.core.*;

class JythonTest 
{
    static main(def args) 
    {
        PythonInterpreter pi;

        pi = new PythonInterpreter()
        pi.exec("from py1 import square3")
        PyFunction pf = (PyFunction)pi.get("square3")

        def map = ["value1":1,"value2":2,"value3":3]     //groovy map   
        PyDictionary pyDict = new PyDictionary(map)
        pf.__call__(pyDict)         //this is line 16 at which according to stack trace the exception is originated (as python function call occurs here)
    }
}

py1.py

def square3(map):
    squareMap = {}
    for k,v in map.items():        #this is line 3 where according to stack trace exception is occurring
        squareMap[k] = v*v
    return squareMap

But I am getting following error:

Exception in thread "main" Traceback (most recent call last):
  File "__pyclasspath__/py1.py", line 3, in square3
java.lang.ClassCastException: java.lang.String cannot be cast to org.python.core.PyObject

    at org.python.core.PyDictionary.dict_items(PyDictionary.java:659)

    at org.python.core.PyDictionary$dict_items_exposer.__call__(Unknown Source)

    at org.python.core.PyObject.__call__(PyObject.java:449)

    at py1$py.square3$1(__pyclasspath__/py1.py:5)

    at py1$py.call_function(__pyclasspath__/py1.py)

    at org.python.core.PyTableCode.call(PyTableCode.java:167)

    at org.python.core.PyBaseCode.call(PyBaseCode.java:138)

    at org.python.core.PyFunction.__call__(PyFunction.java:413)

    at org.python.core.PyFunction.__call__(PyFunction.java:408)

    at org.python.core.PyFunction$__call__.call(Unknown Source)

    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)

    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)

    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:122)

    at JythonTest.main(JythonTest.groovy:16)


java.lang.ClassCastException: java.lang.ClassCastException: java.lang.String cannot be cast to org.python.core.PyObject

In exception stack trace it says the exception is in python file line 3. But when I invoke the python function from python itself, say by attaching below line at the end of py1.py:

a = square3({"val1":1,"val2":2})
print(a) 

I get following output:

{'val2': 4, 'val1': 1}

So why invoking python function from java fails? I am not getting whats going wrong here. Why call pf.__call__(pyDict) throwing such exception?

来源:https://stackoverflow.com/questions/39393713/java-lang-classcastexception-while-using-pythoninterpreter-from-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!