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变量,见附件:
在java application中调用process proc = runtime.getruntime().exec("python xx.py");是可以的【xx.py直接位于工程目录下面】
在tomcat中的servlet中使用process proc = runtime.getruntime().exec("python xx.py");时,开始是没反应。 排查结果应该是pyhon命令可以找到(或者直接输入e:\python\python.exe绝对路径) x.x.py 文件我是放在根目录下,故意把名字写错成xy.py.,结果都是没反应【仿佛这条语句没有执行】
=》应该是py文件找不到。我用了绝对路径搜索py文件。process proc = runtime.getruntime().exec("python d:\xx.py");这个倒是可以。
【目前总结 就是py文件的路径问题】
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号