I am using Jython within a Java project.
I have one Java class: myJavaClass.java
and one Python class: myPythonClass.py
public class myJavaClass{
public String myMethod() {
PythonInterpreter interpreter = new PythonInterpreter();
//Code to write
}
}
The Python file is as follows:
class myPythonClass:
def abc(self):
print "calling abc"
tmpb = {}
tmpb = {'status' : 'SUCCESS'}
return tmpb
Now the problem is I want to call the abc()
method of my Python file from the myMethod
method of my Java file and print the result.
If we need to run a python function that has parameters, and return results, we just need to print this:
somme is the function in python module somme_x_y.py
If I read the docs right, you can just use the
eval
function:Or if you want to get a string:
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:There isn't any way to do exactly that (that I'm aware of).
You do however have a few options:
1) Execute the python from within java like this:
2) You can maybe use Jython which is "an implementation of the Python programming language written in Java", from there you might have more luck doing what you want.
3) You can make the two applications communicate somehow, with a socket or shared file
You can download here Jython 2.7.0 - Standalone Jar.
Then ...
Then...