java直接调用python脚本的例子
程序员文章站
2023-11-16 16:42:52
复制代码 代码如下:import java.io.bufferedreader;import java.io.inputstreamreader;
public clas...
复制代码 代码如下:
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();
}
}
}
如果在eclipse中直接运行报如下错误:
java.io.ioexception: cannot run program "python": createprocess error=2
则配置run configuration中的enviroment,增加path变量,见下图: