Вызов метода Python с использованием Java - PullRequest
0 голосов
/ 03 апреля 2019

Мне нужно получить результат метода Python с использованием Java.Я использую Jython для вызова метода, который я написал на python, просто чтобы напечатать «Hello World».

Это код Python,

class Hello:

    def run(self):
        print('Hello world!')

Это код Java, который я написал для вызова этого метода..

public static void main(String[] args) {
        Properties props = new Properties();
        props.setProperty("python.path", "D:\\Github Repositories\\OntoVec\\");
        PythonInterpreter.initialize(System.getProperties(), props, new String[]{""});
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.exec("from NewModule.TestJython import TestJython");
        PyInstance instance = (PyInstance) interpreter.eval("TestJython()");
        PyMethod method = (PyMethod) instance.__getattr__("run");
        PyObject object = method.__call__();
}

При выполнении этого кода я получаю следующую ошибку.Я реализовал это как проект maven, импортирующий зависимость Jython 2.7.0 в pom.xml.Я делаю это в среде Windows, и «D: \ Github Repositories \ OntoVec \» - это путь к проекту Python.«TestJython» - это имя файла python, а «run» - это имя метода python.

Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site
Determine if the following attributes are correct:
  * sys.path: ['D:\\Github Repositories\\OntoVec\\', 'C:\\Users\\Akila Amarasinghe\\.m2\\repository\\org\\python\\jython\\2.7.0\\Lib', '__classpath__', '__pyclasspath__/']
    This attribute might be including the wrong directories, such as from CPython
  * sys.prefix: C:\Users\Akila Amarasinghe\.m2\repository\org\python\jython\2.7.0
    This attribute is set by the system property python.home, although it can
    be often automatically determined by the location of the Jython jar file

You can use the -S option or python.import.site=false to not import the site module

Может кто-нибудь показать, как вызывать метод python?Пожалуйста, укажите, если я сделал это неправильно.Ценю помощь.

...