我在Linux EC2(ELB)實例上從Java代碼運行PhantomJS進程時遇到問題。相同的代碼在Windows操作系統(我的本地開發)上正常工作,其中PhantomJS進程運行腳本,生成PDF文件並返回期望的0退出值。這些問題僅發生在EC2 Linux實例上。我將簡要介紹迄今爲止我所做的。在AWS EC2上運行Java進程中的PhantomJS 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用很少的修改。
注意:我也試着用/usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs
和其他安裝時創建的系統鏈接替換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建議,我已閱讀錯誤流並最終獲得誤差:
Can't open '/home/ec2-user/reports/renderer.js'
任何想法可能是什麼問題?我遵循這個建議:https://stackoverflow.com/a/22002485/3076403但它沒有解決我的問題。
您可以讀取進程的錯誤流,以查看錯誤消息'p.getErrorStream()' – Titus
我已經使用@Titus建議的結果更新了我的問題。 –