简体   繁体   English

在AWS EC2 Linux上的Java流程中运行PhantomJS

[英]Running PhantomJS in Java process on AWS EC2 Linux

I have a problem to run PhantomJS process from Java code on Linux EC2 (ELB) instance. 我在Linux EC2(ELB)实例上从Java代码运行PhantomJS进程时遇到问题。 The same code works correctly on Windows OS (my local development), where PhantomJS process runs the script, generating the PDF file and returns expected 0 exit value. 相同的代码在Windows操作系统(我的本地开发)上正常工作,其中PhantomJS进程运行脚本,生成PDF文件并返回预期的0退出值。 The problems occurs just on EC2 Linux instance. 该问题仅发生在EC2 Linux实例上。 I will briefly describe what I've done so far. 我将简要介绍到目前为止的工作。

I have succesfully installed the PhantomJS on EC2 instance using this commands: 我已经使用以下命令在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

To check if everything is running correctly, I invoked my desired command from the bash window. 为了检查一切是否正常运行,我从bash窗口中调用了所需的命令。 It generates expected PDF file correctly: 它会正确生成预期的PDF文件:

phantomjs /home/ec2-user/reports/renderer.js "http://localhost:8080/report.html" /home/ec2-user/reports/tmp/report.pdf Letter

Fragment of my source code responsible for invoking PhantomJS process looks like this: 我负责调用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();
}

The renderer.js script is from: https://github.com/ariya/phantomjs/blob/master/examples/rasterize.js with very little modifications. renderer.js脚本来自: https : //github.com/ariya/phantomjs/blob/master/examples/rasterize.js ,只需很少的修改即可。

Note: I tried also to replace the phantomjs command with /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs and other created system links on installation. 注意:我还尝试在安装时用/usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs和其他创建的系统链接替换phantomjs命令。

There is no exception from Java side, while running the above code. 在运行上述代码时,Java方面也不例外。 Just the exit value is not 0 and there is no PDF file generated. 只是退出值不为0,并且没有生成PDF文件。 I expected that the process will be running for a while, but the "error" response occurs just after start. 我预计该进程将运行一段时间,但是“错误”响应仅在启动后发生。

For testing purposes I granted the chmod -R 777 permission for /home/ec2-user/reports/ and also 777 for PhantomJS application stored in /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs . 出于测试目的授予我chmod -R 777权限/home/ec2-user/reports/和777也存储在PhantomJS应用/usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs

I will kindly appreciate your help and all the suggestions. 谢谢您的帮助和所有建议。 Please ask me for further details also, if I omit some important information. 如果我省略一些重要信息,也请问我更多细节。


EDIT: As @Titus suggested, I have read the error stream and finally gets the error: 编辑:正如@Titus所建议,我已经阅读了错误流,最后得到了错误:

Can't open '/home/ec2-user/reports/renderer.js' 无法打开“ /home/ec2-user/reports/renderer.js”

Any ideas what might be the issue? 任何想法可能是什么问题? I followed this suggestion: https://stackoverflow.com/a/22002485/3076403 but it didn't resolve my problem. 我遵循了这个建议: https : //stackoverflow.com/a/22002485/3076403,但是它不能解决我的问题。

I finally resolved this issue by myself. 我终于自己解决了这个问题。 To sum up, the error from PhantomJS process invoked from Java code was: 总而言之,从Java代码调用的PhantomJS进程中的错误是:

Can't open '/home/ec2-user/reports/renderer.js'

Some people who will have this issue may follow this suggestion: https://stackoverflow.com/a/22002485/3076403 but it didn't resolve my problem. 可能会有此问题的某些人可能会遵循以下建议: https : //stackoverflow.com/a/22002485/3076403但这不能解决我的问题。

In my case, the problem was that I store my script file runned from PhantomJS in /home/ec2-user/reports/renderer.js , but after I move this script file to /var/local/renderer.js it runs correctly from Java process call. 就我而言,问题是我将从PhantomJS运行的脚本文件存储在/home/ec2-user/reports/renderer.js ,但是在将该脚本文件移至/var/local/renderer.js它可以从Java流程调用。 I didn't know that: 我不知道:

/var/local -> stores variable data for local programs (i.e., programs that have been installed by the system administrator) that are installed in /usr/local.

Quoted from: http://www.tldp.org/LDP/Linux-Filesystem-Hierarchy/html/var.html 引用自: http : //www.tldp.org/LDP/Linux-Filesystem-Hierarchy/html/var.html

Fortunately, also storing the generated PDF files in /var/local/tmp directory allows to fetch these files from the Java application, which was not possible from /home/ec2-user/reports/tmp folder (no additional directory/file permissions were required). 幸运的是,还可以将生成的PDF文件存储在/var/local/tmp目录中,从而可以从Java应用程序中获取这些文件,而这在/home/ec2-user/reports/tmp文件夹中是不可能的(没有其他目录/文件许可权需要)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM