我有一个问题:我必须在java程序中执行一个phantomjs脚本,但我不能。这是脚本的代码:
var page = require('webpage').create();
page.open('http://stackoverflow.com',function(status){
if(status !== 'success'){
console.log('Open failed');
}else{
console.log(page.evaluate(function(){
return document.documentElement.outerHTML;
}));
}
phantom.exit();
});
在实践中,我想执行网页的javascript,然后获取html结果。我尝试使用此链接中的代码从javascript,JSP或Java运行Phantomjs,但是我的程序在此行被阻止
int exitStatus = process.waitFor();
我该怎么做才能解决这个问题?
谢谢
您可以使用运行时类来执行终端命令。我能够使用以下程序生成我想要的输出。
Runtime rt = Runtime.getRuntime();
try{
Process pr = rt.exec("pathToPhantomHome/phantomjs "+ " pathToJSfile");
BufferedInputStream bis = new BufferedInputStream(pr.getInputStream());
bis.close();
}catch(Exception ex){
ex.printStackTrace();
}