1

My python File has the code as:

class myPythonClass():
def abc(self):
    print "calling abc"
    tmpb = {}
    tmpb = {'status' : 'SUCCESS'}
    return tmpb

and my java code is:

 PythonInterpreter interpreter = new PythonInterpreter();
             interpreter.execfile("/home/Desktop/abc.py");
             interpreter.set("myvariable", file1);
             PyObject answer = interpreter.eval("myPythonClass().abc(myvariable)");
             System.out.println(answer.toString());

In my python file if i dont give brackets for class name then my java code executes python file. But if my python file starts with class myPythonClass(): then its not working. Can anyone help me out in this?

bjk
  • 39
  • 3

2 Answers2

0

Try identing:

class myPythonClass():
    def abc(self):
        print "calling abc"
        tmpb = {}
        tmpb = {'status' : 'SUCCESS'}
        return tmpb

It seems it isn't indented so it doesn't becomes part of the class declaration and Python doesn't likes that. But I know nothing about Java so...

Also try starting with a comment (#comment) and see if it works.

PS: Sorry if I wasn't clear or sure, I just can't comment with the reputation I has.

wallabra
  • 412
  • 8
  • 17
0

Can you have a look at how-to-call-a-python-method-from-a-java-class.

Until your python code is correct and intended correctly. If I read the docs right, you can just use the eval function: :)

interpreter.execfile("/path/to/python_file.py");
PyDictionary result = interpreter.eval("myPythonClass().abc()");

Or if you want to get a string:

PyObject str = interpreter.eval("repr(myPythonClass().abc())");
System.out.println(str.toString());

If you want to supply it with some input from Java variables, you can use set beforehand and than use that variable name within your Python code:

interpreter.set("myvariable", Integer(21));
PyObject answer = interpreter.eval("'the answer is: %s' % (2*myvariable)");
System.out.println(answer.toString());
Community
  • 1
  • 1
Furqan Aziz
  • 1,094
  • 9
  • 18