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.
p.getErrorStream()