Common ways for java to invoke python scripts
- Implemented through a class library provided by jython.jar
- Execute the script file by opening the process with Runtime.getRuntime()
Implemented through a class library provided by jython.jar
To do this with jython.jar, we need to import the jar package. Specifically, I wrote a demo, assuming your python code is test.py:
def my_test(name, age):
print("name: "+name)
print("age: "+age)
return "success"
- 1
- 2
- 3
- 4
java calls test.py code:
public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("E:\\workspace\\pycharm_workspace\\weixincrawer\\test.py");
PyFunction function = (PyFunction)interpreter.get("my_test",PyFunction.class);
PyObject pyobject = function.__call__(new PyString("huzhiwei"),new PyString("25"));
System.out.println("anwser = " + pyobject.toString());
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
Output results:
name: huzhiwei
age: 25
anwser = success
- 1
- 2
- 3
There is no problem at this point. We call the python function with the function.call method passed in parameter, and use the pyobject.toString() method to get the return value of my_test function in python, but if you make a slight change to test.py as follows:
import requests
def my_test(name, age):
response = requests.get("http://www.baidu.com")
print("name: "+name)
print("age: "+age)
return "success"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
Without modifying the java call code, you will get the following exception information:
ImportError: No module named requests
- 1
Yes, that's what I'm going to talk about, because jython can't cover all of python's third-party class libraries, so when we use the requests class library in our Python files, it's obvious that we'll report an error that we can't find the module, at which point we can execute Python script files by starting the process with Runtime.getRuntime().
Execute the script file by opening the process with Runtime.getRuntime()
Using this method requires both modifications to the python file and the java call code, which I also modified on the basis of test.py above:
import requests
import sys
def my_test(name, age):
response = requests.get("http://www.baidu.com")
print("url: "+response.url)
print("name: "+name)
print("age: "+age)
return "success"
my_test(sys.argv[1], sys.argv[2])
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
The biggest difference with the above test.py code is that the way we start the process here actually operates on an invisible dos call interface, so in python code we need sys.argv to get the parameters passed in the java code.
java call code section:
public static void main(String[] args) {
String[] arguments = new String[] {"python", "E:\\workspace\\pycharm_workspace\\weixincrawer\\test.py", "huzhiwei", "25"};
try {
Process process = Runtime.getRuntime().exec(arguments);
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
int re = process.waitFor();
System.out.println(re);
} catch (Exception e) {
e.printStackTrace();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
Output of results:
url: http://www.baidu.com/
name: huzhiwei
age: 25
0
- 1
- 2
- 3
- 4
One thing to note here is that a return value of 0 for process.waitFor() in java code indicates that we have successfully called the python script, and a return value of 1 indicates that the call to the python script failed, as opposed to what we normally see as a definition of 0 and 1.
From: https://blog.csdn.net/hzw19920329/article/details/77509497