package python;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.python.antlr.PythonParser.test_return;
import org.python.util.PythonInterpreter;
public class java_python {
public static void main(String[] args) {
// System.out.println("hello word");
// PythonInterpreter interpreter = new PythonInterpreter();
// interpreter.exec("a=[5,2,3,9,4,0]; ");
// interpreter.exec("print(sorted(a));"); //此处python语句是3.x版本的语法
// interpreter.exec("print sorted(a);"); //此处是python语句是2.x版本的语法
// interpreter.execfile("C:\\Users\\张家豪\\Desktop\\test.py");
Process proc;
try {
proc = Runtime.getRuntime().exec("python C:\\Users\\张家豪\\Desktop\\test.py");
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
proc.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
我的python脚本