简体   繁体   English

在java程序中执行phantomjs脚本

[英]Execute phantomjs script in java program

I have a problem: I have to execute a phantomjs script in java program but I can't. 我有一个问题:我必须在java程序中执行一个phantomjs脚本,但我不能。 this is the code of the script: 这是脚本的代码:

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();
});

In practice, I want to execute the javascript of a web page and after take the html resultant. 在实践中,我想执行网页的javascript并在获取html结果之后。 I tried with the code at this link Running Phantomjs from javascript, JSP or Java but my program is blocked at this line 我尝试使用javascript,JSP或Java上运行Phantomjs的链接中的代码,但我的程序在此行被阻止

int exitStatus = process.waitFor();

what can I do to resolve this?? 我该怎么做才能解决这个问题?

Thanks 谢谢

You can use Runtime class to execute terminal commands. 您可以使用Runtime类来执行终端命令。 I was able to generate my desired output using below program. 我能够使用以下程序生成我想要的输出。

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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM