Jython - вызов класса Python в Java - PullRequest
0 голосов
/ 08 ноября 2018

Я хочу вызвать свой класс Python на Java, но я получаю сообщение об ошибке:

Manifest com.atlassian.tutorial: myConfluenceMacro: Atlassian-плагин: 1.0.0-SNAPSHOT : Классы найдены в неправильном каталоге

Я установил Jython на свой компьютер через jar. И добавил его в мой pom (потому что я использую проект Maven). Что я делаю неправильно? Как я могу вызвать метод Python внутри моего Java-класса?
Я использую python3 .

POM

<!-- https://mvnrepository.com/artifact/org.python/jython-standalone -->
<dependency>
    <groupId>org.python</groupId>
    <artifactId>jython-standalone</artifactId>
    <version>2.7.1</version>
</dependency>

JAVA CLASS

package com.atlassian.tutorial.javapy;
import org.python.core.PyInstance;  
import org.python.util.PythonInterpreter;  


public class InterpreterExample  
{  

   PythonInterpreter interpreter = null;  


   public InterpreterExample()  
   {  
      PythonInterpreter.initialize(System.getProperties(),  
                                   System.getProperties(), new String[0]);  

      this.interpreter = new PythonInterpreter();  
   }  

   void execfile( final String fileName )  
   {  
      this.interpreter.execfile(fileName);  
   }  

   PyInstance createClass( final String className, final String opts )  
   {  
      return (PyInstance) this.interpreter.eval(className + "(" + opts + ")");  
   }  

   public static void main( String gargs[] )  
   {  
      InterpreterExample ie = new InterpreterExample();  

      ie.execfile("hello.py");  

      PyInstance hello = ie.createClass("Hello", "None");  

      hello.invoke("run");  
   }  


} 

Класс Python

class Hello:  
    __gui = None  

def __init__(self, gui):  
    self.__gui = gui  

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

Спасибо!

1 Ответ

0 голосов
/ 08 ноября 2018

У вас неправильный отступ в вашем классе Python. Правильный код:

class Hello:  
    __gui = None  


    def __init__(self, gui):  
        self.__gui = gui  


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

, чтобы __init__() и run() были методами вашего класса Hello, а не глобальными функциями. В противном случае ваш код работает хорошо.

И, пожалуйста, помните, что последняя версия Jython 2.7.1 - она ​​не совместима с Python3.

...