Runtime.getRuntime().exec(...)使用方法
Runtime.getRuntime().exec(...)使用方法
如果想要了解更多的信息,参阅代码里面给的链接
下面是这个正确的例子
- public class RuntimeExec {
- /**
- * Runtime execute.
- *
- * @param cmd the command.
- * @return success or failure
- * @see {@link http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4}
- * @since 1.1
- */
- public static boolean runtimeExec(String cmd) {
- try {
- Process proc = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", cmd});
- // any error message?
- StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
- // any output?
- StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
- ) {
- System.err.println("执行\"" + cmd + "\"时返回值=" + proc.exitValue());
- return false;
- } else {
- return true;
- }
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- static class StreamGobbler extends Thread {
- InputStream is;
- String type;
- StreamGobbler(InputStream is, String type) {
- this.is = is;
- this.type = type;
- }
- public void run() {
- try {
- InputStreamReader isr = new InputStreamReader(is);
- BufferedReader br = new BufferedReader(isr);
- String line = null;
- while ((line = br.readLine()) != null)
- System.out.println(type + ">" + line);
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
- }
- }
Runtime.getRuntime().exec(...)使用方法的更多相关文章
- 关于Runtime.getRuntime().exec()产生阻塞的2个陷阱
本文来自网易云社区 背景 相信做java服务端开发的童鞋,经常会遇到Java应用调用外部命令启动一些新进程来执行一些操作的场景,这时候就会使用到Runtime.getRuntime().exec(), ...
- 使用Runtime.getRuntime().exec()方法的几个陷阱 (转)
Process 子类的一个实例,该实例可用来控制进程并获得相关信息.Process 类提供了执行从进程输入.执行输出到进程.等待进程完成.检查进程的退出状态以及销毁(杀掉)进程的方法. 创建进程的方法 ...
- [转]使用Runtime.getRuntime().exec()方法的几个陷阱
Process 子类的一个实例,该实例可用来控制进程并获得相关信息.Process 类提供了执行从进程输入.执行输出到进程.等待进程完成.检查进程的退出状态以及销毁(杀掉)进程的方法. 创建进程的方法 ...
- 使用Runtime.getRuntime().exec()方法的几个陷阱
Process 子类的一个实例,该实例可用来控制进程并获得相关信息.Process 类提供了执行从进程输入.执行输出到进程.等待进程完成.检查进程的退出状态以及销毁(杀掉)进程的方法. 创建进程的方法 ...
- Runtime.getRuntime().exec方法
Runtime.getRuntime().exec()方法主要用于执行外部的程序或命令. Runtime.getRuntime().exec共有六个重载方法: public Process exec( ...
- 用Runtime.getRuntime().exec()需要注意的地方
有时候我们可能需要调用系统外部的某个程序,此时就可以用Runtime.getRuntime().exec()来调用,他会生成一个新的进程去运行调用的程序. 此方法返回一个java.lang.Proce ...
- Runtime.getRuntime().exec中命令含有括号问题
在写批量运行bat工具的时候.想起了之前写的定时小工具里面的运行方法. 使用Runtime.getRuntime().exec方法. Runtime.getRuntime().exec("c ...
- [转]java调用外部程序Runtime.getRuntime().exec
Runtime.getRuntime().exec()方法主要用于执行外部的程序或命令. Runtime.getRuntime().exec共有六个重载方法: public Process exec( ...
- Runtime.getRuntime.exec();
杀死Chrome浏览器进程 private static void closeAllChrome() throws IOException{ Runtime.getRuntime().exec(&qu ...
随机推荐
- JSONP跨域的原理解析及其实现介绍
JSONP跨域的原理解析及其实现介绍 作者: 字体:[增加 减小] 类型:转载 时间:2014-03-22 JSONP跨域GET请求是一个常用的解决方案,下面我们来看一下JSONP跨域是如何实现的,并 ...
- offsetParent 到底是哪一个?
前言 温故而知新.遇到offsetParent这个知识点,发现书上讲的不够详细.于是看了看豪情的博客,发现讲得很具体,收藏一下. 正文 不同情况 没有已定位的父节点,且自身position:relat ...
- JS封装移动端触摸滑动插件应用于导航banner【精装版】
自己封装了一个小插件,一个小尝试. 可用于类似于京东导航等效果,效果多样化,很方便. 欢迎大家提点意见. mrChenSwiper( {parent, child, type, parentN, c ...
- HOOK API(四)—— 进程防终止
HOOK API(四) —— 进程防终止 0x00 前言 这算是一个实战吧,做的一个应用需要实现进程的防终止保护,查了相关资料后决定用HOOK API的方式实现.起初学习HOOK API ...
- Android应用开发提高篇(3)-----传感器(Sensor)编程
链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/29/2373420.html 一.概述 Android支持的传感器种类越来越多了,这确实是一件可 ...
- .net DataTable 取值辅助类
DataTableCommon类主要是帮助取值 方法列表: public static string GetCellString(DataTable dt,int row, int column) p ...
- Bootstrap 响应式瀑布流 (使用wookmark)
使用瀑布布局 官方 http://www.wookmark.com/jquery-plugin GitHub https://github.com/GBKS/Wookmark-jQuery (下载后 ...
- 清除缓存、开启IO统计
SQL性能优化前期准备-清除缓存.开启IO统计 如果需要进行SQl Server下的SQL性能优化,需要准备以下内容: 一.SQL查询分析器设置: 1.开启实际执行计划跟踪. 2.每次执行需优化SQL ...
- Linux计时体系结构
[Linux操作系统分析]定时测量——RTC,TSC,PIT,jiffies,计时体系结构,延迟函数 1 基本概念 定时机制连同一些更可见的内核活动(如检查超时)来驱使进程切换. 两种主要的定时测 ...
- Spring Boot Memory Performance
The Performance Zone is brought to you in partnership with New Relic. Quickly learn how to use Docke ...