1

I have a problem to run PhantomJS process from Java code on Linux EC2 (ELB) instance. 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. The problems occurs just on EC2 Linux instance. I will briefly describe what I've done so far.

I have succesfully installed the PhantomJS on EC2 instance using this commands:

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. It generates expected PDF file correctly:

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:

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.

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.

There is no exception from Java side, while running the above code. Just the exit value is not 0 and there is no PDF file generated. 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.

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:

Can't open '/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.

2
  • 1
    You can read the process' error stream to see the error message p.getErrorStream() Commented Jun 15, 2015 at 15:35
  • I have updated my question with the result from @Titus suggestion. Commented Jun 16, 2015 at 8:59

1 Answer 1

0

I finally resolved this issue by myself. To sum up, the error from PhantomJS process invoked from Java code was:

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.

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. 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

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.