user3229956
user3229956

Reputation: 11

Running PhantomJs in java

I'm trying to run phantomjs in my program, below is the command I'm running.

Process exec = Runtime.getRuntime().exec(new String[]{
            "C:/Users/buddy/Desktop/phantomjs-1.9.7-windows",
            "-c",
            "phantomjs "
                + pipedCommand + " "
                + size + " "
                + conversion.getConversionTarget().extension() + " "
                + this.local.getId()});

and I'm getting exception when I run: (But above code runs for Linux)

I have downloaded phantomjs windows version and its in the following path: C:/Users/buddy/Desktop/phantomjs-1.9.7-windows

java.io.IOException: Cannot run program "C:/Users/buddy/Desktop/phantomjs-1.9.7-windows": CreateProcess error=5, Access is denied.

Upvotes: 0

Views: 4527

Answers (2)

Lalith J.
Lalith J.

Reputation: 1391

You can run PhantomJS using Selenium.

        DesiredCapabilities caps = new DesiredCapabilities();
        String path = "Path to PhantomJS Binary";
        caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, path);
        WebDriver driver = new PhantomJSDriver(caps);

Upvotes: 0

dermd
dermd

Reputation: 167

I believe you need to run it from a cmd process... not sure if it helps but how I did it was to create a shellscript file with contents similar to below

echo "- Processing : Creating screenshot image from $1"
'C:\software\phantomjs' 'C:\software\rasterize.js' http://$1 $2

and then within the java code

// Create process command
String command = "cmd /c " + imageScriptLocation + " " + cleanUrl + " " +  imgLocation
// Execute the process
Runtime rt = Runtime.getRuntime();
p = rt.exec(command);
// Retrieve any errors from the script
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
    logger.debug("Image Script: " + line);
}

// Wait for the process to complete and the images to be generated
p.waitFor();

if you just want to use your approach above though, the below should work...

Process exec = Runtime.getRuntime().exec(new String[]{
            "cmd", 
            "/c",
            "phantomjs "
                + pipedCommand + " "
                + size + " "
                + conversion.getConversionTarget().extension() + " "
                + this.local.getId()});

Upvotes: 1

Related Questions