Home  >  Article  >  Backend Development  >  JAVA adjust python script

JAVA adjust python script

巴扎黑
巴扎黑Original
2016-12-07 10:37:161170browse

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]

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn