在java中运行PhantomJs
我试图在我的程序中运行phantomjs,下面是我正在运行的命令在java中运行PhantomJs,phantomjs,java-7,Phantomjs,Java 7,我试图在我的程序中运行phantomjs,下面是我正在运行的命令 Process exec = Runtime.getRuntime().exec(new String[]{ "C:/Users/buddy/Desktop/phantomjs-1.9.7-windows", "-c", "phantomjs " + pipedCommand + " " + si
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()});
当我运行时,我得到了一个异常:(但上面的代码是针对Linux运行的)
我已在以下路径下载了phantomjs windows版本及其应用程序: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.
我相信你需要从cmd进程运行它。。。不确定这是否有帮助,但我是如何创建一个包含如下内容的shellscript文件的
echo "- Processing : Creating screenshot image from $1"
'C:\software\phantomjs' 'C:\software\rasterize.js' http://$1 $2
然后在java代码中
// 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();
如果你只是想使用上面的方法,下面的方法应该是有效的
Process exec = Runtime.getRuntime().exec(new String[]{
"cmd",
"/c",
"phantomjs "
+ pipedCommand + " "
+ size + " "
+ conversion.getConversionTarget().extension() + " "
+ this.local.getId()});
您可以使用运行PhantomJS
DesiredCapabilities caps = new DesiredCapabilities();
String path = "Path to PhantomJS Binary";
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, path);
WebDriver driver = new PhantomJSDriver(caps);