Invoke Python modules from Java

后端 未结 3 1572
心在旅途
心在旅途 2020-12-31 21:55

I have a Python interface of a graph library written in C - igraph (the name of library). My need is to invoke the python modules pertaining to this graph library from Java

3条回答
  •  有刺的猬
    2020-12-31 22:23

    You can use jep.getValue() to retrieve a value from script's global dictionary.

    There are caveats to that concerning scope levels in Python, so most people find it clearer to pass a Java class to python and set the return value in that instance in Python. After the script completes, the Java code will then have the result.

    For example:

    ==> Java

    class ReturnValueClass {
       public int scriptResult;
    };
    ReturnValueClass value = new ReturnValueClass();
    jep.set("retval", value);
    

    ==> Python

    # do something
    pass
    # write the return value
    retval.scriptResult = some_python_value
    

    ==> Java

    System.out.println(value.scriptResult);
    

    Hope that helps,

    Mike (I wrote Jep)

提交回复
热议问题