Java执行命令行操作的正确方法是什么?
- 内容介绍
- 文章标签
- 相关推荐
本文共计226个文字,预计阅读时间需要1分钟。
示例:使用phantomjs抓取百度网页特定区域的内嵌,Java代码通过Runtime.getRuntime().exec()实现命令行操作。首先需下载phantomJS.js代码:var page=require('webpage').create(); var address='https://www.baidu.com/';
首先需要下载phantomJS js代码: var page=require('webpage').create(); var address='www.baidu.com/'; // 设置url var output='kiban.png'; // 设置保存文件名 page.viewportSize={width:1024,height:800}; // 设置查看页面的分辨率 page.open(address,function(status){ if(status!=='success'){ console.log('Unabletoloadtheaddress!'); phantom.exit(); }else{ window.setTimeout(function(){ page.clipRect={top:170, left:10, height: 330, width: 980}; // 设置页边距,从而获取想要的图片,需要慢慢调整 page.render(output); // 保存图片 phantom.exit(); },20000); } }); 脚本代码testOne: F: cd F:\\phantomjsWork F:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe kibanaTwo.js java代码: public class TestOne { public static void main(String[] args) { String path = "F:\\TestOne.bat"; Runtime run = Runtime.getRuntime(); try { Process process = run.exec("cmd.exe /k start " + path); process.waitFor(); } catch (Exception e) { e.printStackTrace(); } } }
本文共计226个文字,预计阅读时间需要1分钟。
示例:使用phantomjs抓取百度网页特定区域的内嵌,Java代码通过Runtime.getRuntime().exec()实现命令行操作。首先需下载phantomJS.js代码:var page=require('webpage').create(); var address='https://www.baidu.com/';
首先需要下载phantomJS js代码: var page=require('webpage').create(); var address='www.baidu.com/'; // 设置url var output='kiban.png'; // 设置保存文件名 page.viewportSize={width:1024,height:800}; // 设置查看页面的分辨率 page.open(address,function(status){ if(status!=='success'){ console.log('Unabletoloadtheaddress!'); phantom.exit(); }else{ window.setTimeout(function(){ page.clipRect={top:170, left:10, height: 330, width: 980}; // 设置页边距,从而获取想要的图片,需要慢慢调整 page.render(output); // 保存图片 phantom.exit(); },20000); } }); 脚本代码testOne: F: cd F:\\phantomjsWork F:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe kibanaTwo.js java代码: public class TestOne { public static void main(String[] args) { String path = "F:\\TestOne.bat"; Runtime run = Runtime.getRuntime(); try { Process process = run.exec("cmd.exe /k start " + path); process.waitFor(); } catch (Exception e) { e.printStackTrace(); } } }

