【发布时间】:2016-02-28 16:23:50
【问题描述】:
对于想要扩展的现有 Java 代码,我需要在 Java 中运行 python 代码。我为此使用流程构建器:
ProcessBuilder pb = new ProcessBuilder("python", "/directorypath/mypython.py");
Process p=pb.start();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
System.exit(0);
}
虽然代码从命令行运行得非常好,但在 Java 中我收到“我们需要 BeautifulSoup,抱歉”错误。据我了解,这意味着 Java 环境缺少某种库,但是代码可以从命令行完美运行。
我需要发送一些环境变量吗?如果是,是什么以及如何?
我安装了“BeautifulSoup4”并且它是最新的。
【问题讨论】:
-
您确定该命令的最后一个参数吗?看起来很奇怪
-
它们只是一些要传递的参数。此代码在命令行上运行良好。
-
是的,但我相信您传递错误;除非你的命令行确实看起来像
python /directorypath/mypython.py "-A 'A Tool for binary' -c 1"。ProcessBuilder不是 shell 解释器! -
好的,但是即使我没有这些参数就通过了 [ ProcessBuilder pb = new ProcessBuilder("python", "/directorypath/mypython.py")] ;我再次收到相同的“我们需要 BeautifulSoup,抱歉”错误。我编辑了这个问题。谢谢!
标签: java python beautifulsoup processbuilder