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?

1
  • "its not working". How? Any error messages? Weird behavior? Unexpected output? Commented Nov 30, 2015 at 13:43

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

No its indented properly at my side
Its saying source file attachment does not contain file_bullitin class
My bad. It seems it don't have a built-in class file (my guess). But as I said, I know nothing about Java.
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());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.