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 ...
随机推荐
- Android 支付宝钱包手势password裂纹战斗
底 随着移动互联网和手机屏幕越做越大的普及等..购物在移动设备上.消费是必不可少的人们习惯于生活. 随着这股浪潮的兴起,安全.便捷的移动支付的需求也越来越大.故,各大互联网公司纷纷推出了移动支付平台. ...
- Jquery ajax调用后台aspx后台文件方法(不是ashx)
在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下. (1)通过aspx.cs的静态方法+WebMethod进行处理 简单的介绍下 ...
- iOS 设置状态栏的颜色
1.在plist文件中设置如下属性: 2.在delegate中设置 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarSt ...
- ELK(ElasticSearch, Logstash, Log4j)系统日志搭建
1.elk平台介绍 Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等. Logsta ...
- HDU1171-Big Event in HDU
描述: Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don ...
- openGL 旋转的图形 矩阵操作
#include <windows.h> #ifdef __APPLE__ #include <GLUT/glut.h> #else #include <GL/glut. ...
- [LeetCode]题解(python):124-Binary Tree Maximum Path Sum
题目来源: https://leetcode.com/problems/binary-tree-maximum-path-sum/ 题意分析: 给定一棵树,找出一个数值最大的路径,起点可以是任意节点或 ...
- MacOS下使用VMware5 破解 安装win7 ISO 激活
VMware5 下载 破解 以及win7 ISO版本的安装 激活VMware5 下载与破解参考方法http://www.macx.cn/thread-2060440-1-1.htmlVMware5 是 ...
- Qt中QPushButton、QTabWidget等部件内容文字换行的解决方法(一定要写在源码里,写在翻译文件里不行,因为\n得不到转义)
最近在做基于Qt的应用项目,其中有一个按钮文字超长,需要换一行显示.从网上找到的建议都是将要显示的内容做成图片,然后将图片设置为该按钮的背景.但是这样做有一个很大的局限性,当需要更改内容文字的时候,需 ...
- SQL中采用Newtonsoft.Json处理json字符串
原文 SQL中采用Newtonsoft.Json处理json字符串 使用环境: SQL Server2005; VS2010; 关于SQL中部署CLR程序集的方法,网上一搜一大把,需要了解的自行查阅, ...