【发布时间】:2015-06-15 15:28:23
【问题描述】:
我在 Linux EC2 (ELB) 实例上从 Java 代码运行 PhantomJS 进程时遇到问题。相同的代码在 Windows 操作系统(我的本地开发)上正常工作,其中 PhantomJS 进程运行脚本,生成 PDF 文件并返回预期的 0 退出值。问题仅发生在 EC2 Linux 实例上。我将简要描述一下我到目前为止所做的事情。
我已经使用以下命令在 EC2 实例上成功安装了 PhantomJS:
cd /usr/local/share
sudo wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-x86_64.tar.bz2
sudo tar xjf phantomjs-1.9.7-linux-x86_64.tar.bz2
sudo ln -s /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /usr/local/share/phantomjs
sudo ln -s /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs
sudo ln -s /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /usr/bin/phantomjs
为了检查一切是否正常运行,我从 bash 窗口调用了我想要的命令。它会正确生成预期的 PDF 文件:
phantomjs /home/ec2-user/reports/renderer.js "http://localhost:8080/report.html" /home/ec2-user/reports/tmp/report.pdf Letter
我负责调用 PhantomJS 进程的源代码片段如下所示:
try {
final ProcessBuilder pb = new ProcessBuilder("phantomjs", "/home/ec2-user/reports/renderer.js", "http://localhost:8080/report.html", "/home/ec2-user/reports/tmp/report.pdf", "Letter");
final Process p = pb.start();
p.waitFor();
final int exitValue = p.exitValue();
if (exitValue == 0) {
LOG.info("PhantomJS has produced valid PDF");
} else {
LOG.debug("PhantomJS failed to produce PDF file");
}
} catch (final InterruptedException | IOException e) {
e.printStackTrace();
}
renderer.js 脚本来自:https://github.com/ariya/phantomjs/blob/master/examples/rasterize.js,几乎没有修改。
注意:我还尝试将phantomjs 命令替换为/usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs 和其他安装时创建的系统链接。
运行上述代码时,Java 方面也不例外。只是退出值不是 0 并且没有生成 PDF 文件。我预计该进程将运行一段时间,但“错误”响应发生在启动后。
出于测试目的,我为/home/ec2-user/reports/ 授予了chmod -R 777 权限,并为存储在/usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs 中的PhantomJS 应用程序授予了777。
非常感谢您的帮助和所有建议。如果我遗漏了一些重要信息,请向我询问更多详细信息。
编辑:正如@Titus 建议的那样,我已经阅读了错误流并最终得到了错误:
无法打开'/home/ec2-user/reports/renderer.js'
任何想法可能是什么问题?我遵循了这个建议:https://stackoverflow.com/a/22002485/3076403,但它并没有解决我的问题。
【问题讨论】:
-
您可以阅读进程的错误流以查看错误消息
p.getErrorStream() -
我已经用@Titus 建议的结果更新了我的问题。
标签: java linux amazon-ec2 phantomjs