Home > Article > Backend Development > JAVA adjust python script
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
try {
System.out.println("start");
Process pr = Runtime.getRuntime().exec("python test.py");
BufferedReader in = new BufferedReader(new InputStreamReader(
pr.getInputStream()));
String line;
while ((line = in.readLine ()) != null) {
System.out.println(line);
}
in.close();
pr.waitFor();
System.out.println("end");
} catch ( Exception e) {
e.printStackTrace();
}
}
}
If run directly in eclipse, the following error will be reported:
java.io.IOException: Cannot run program "python": CreateProcess error=2
Configure Run Environment in Configuration, add the PATH variable, see attachment:
It is OK to call Process proc = Runtime.getRuntime().exec("python xx.py"); in the java application [xx.py is located directly under the project directory ]
When using Process proc = Runtime.getRuntime().exec("python xx.py"); in the servlet in tomcat, there is no response at first. The troubleshooting result should be that it can be found with the pyhon command (or directly enter the absolute path of e:\Python\python.exe). I placed the x.x.py file in the root directory and deliberately wrote the wrong name as xy.py., but the result was no response. [It seems that this statement is not executed]
=》It should be that the py file cannot be found. I used absolute paths to search for py files. Process proc = Runtime.getRuntime().exec("python d:\xx.py"); This is fine.
[The current summary is the path problem of the py file]