Runtime.getRuntime().exec();里面产生停滞(阻塞,blocking) 标签: Process process=Runtime.getRuntime()里面产生停滞 2007-02-28 11:03 Process process=Runtime.getRuntime().exec("");中产生停滞(阻塞,blocking),怎么解决? --------------------------------------------------------------- 这个是因为Runtime.getRuntime().exec()要自己去处理stdout和stderr的。 所以如果你想让程序正常运行的话,请务必将上述用别的线程流取走。 >test.bat haha exit 99 >RuntimeTest.java public class RuntimeTest { public static void main(String[] args) { try { Process process=Runtime.getRuntime().exec("test.bat"); StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR"); // kick off stderr errorGobbler.start(); StreamGobbler outGobbler = new StreamGobbler(process.getInputStream(), "STDOUT"); // kick off stdout outGobbler.start(); process.waitFor(); System.out.println(process.exitValue()); } catch(Exception e) {} } } >StreamGobbler.java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; public class StreamGobbler extends Thread { InputStream is; String type; OutputStream os; StreamGobbler(InputStream is, String type) { this(is, type, null); } StreamGobbler(InputStream is, String type, OutputStream redirect) { this.is = is; this.type = type; this.os = redirect; } public void run() { try { PrintWriter pw = null; if (os != null) pw = new PrintWriter(os); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line=null; while ( (line = br.readLine()) != null) { if (pw != null) pw.println(line); System.out.println(type + ">" + line); } if (pw != null) pw.flush(); } catch (IOException ioe) { ioe.printStackTrace(); } } }

Runtime.getRuntime().exec();里面产生停滞(阻塞,blocking)相关推荐

  1. java runtime.exec 阻塞_关于Runtime.getRuntime().exec()产生阻塞的2个陷阱

    背景 相信做java服务端开发的童鞋,经常会遇到Java应用调用外部命令启动一些新进程来执行一些操作的场景,这时候就会使用到Runtime.getRuntime().exec(),然而这个方法如果不谨 ...

  2. 如何在JAVA代码中执行 exec master..xp_cmdshell @cmd // 当作SQL语句调用就成了 或者调用 Runtime.getRuntime().exec

    例如: //package FS; import java.io.IOException; import java.sql.*; public class BCP {  static Connecti ...

  3. java Runtime.getRuntime().exec 获取反弹shell

    说明 前面写了一篇在Java环境下获取shell的文章.当时使用的语句是: Runtime r = Runtime.getRuntime(); Process p = r.exec(new Strin ...

  4. JAVA命令运行cmd命令得到的结果乱码Runtime.getRuntime().exec();

    Process process = Runtime.getRuntime().exec("cmd /c dir c:"); BufferedReader bufferedReade ...

  5. Runtime.getRuntime().exec(...)使用方法

    Runtime.getRuntime().exec(...)使用方法 如果想要了解更多的信息,参阅代码里面给的链接 下面是这个正确的例子 public class RuntimeExec { /** ...

  6. Java开发:(执行系统(例cmd)命令)Runtime.getRuntime().exec()参数解释

    在日常的java编程开发中,难免遇到要在程序中使用系统的操作命令,如windows下的cmd,或linux的命令,接下来介绍以下Runtime.getRuntime().exec()函数所执行的系统命 ...

  7. Runtime.getRuntime().exec

    转载:http://blog.sina.com.cn/s/blog_3d731e9001000ajm.html 今天发现了Runtime.getRuntime().exec()新的使用方法: //测试 ...

  8. java执行python脚本_使用Runtime.getRuntime().exec()在java中调用python脚本

    举例有一个Python脚本叫test.py,现在想要在Java里调用这个脚本.假定这个test.py里面使用了拓展的包,使得pythoninterpreter之类内嵌的编译器无法使用,那么只能采用ja ...

  9. Runtime.getRuntime().exec()调用外部程序

    场景:linux下,在web工程里调用一个C++程序,实现代码如下: StringBuffer cmd = new StringBuffer(); cmd.append("nohup &qu ...

最新文章

  1. 用python解“BCD解密”问题
  2. markdown语法中的空格_Markdown常用语法汇总
  3. SQL多表连接查询(详细实例)
  4. python对文件的读操作方法有哪些-Python文件操作实例大全
  5. 小学听课计算机笔记范文,小学教师听课记录精选10篇【最新】.pdf
  6. ORACLE逻辑DATAGUARD创建表
  7. 一行代码不用写,就可以训练、测试、使用模型,这个 star 量 1.5k 的项目帮你做到...
  8. 用 Identity Server 4 (JWKS 端点和 RS256 算法) 来保护 Python web api
  9. android studio ignore 模板,android studio git ignore
  10. jQuery实现数据映入form表单
  11. java map对象排序输出_java实现对象的排序(List和Map)
  12. JavaScript:画廊案例
  13. java protected_Java中的访问修饰符详解
  14. DVWA教程(一) —— Low级别
  15. [转载]世界各个地区WIFI 2.4G及5G信道划分表(附无线通信频率分配表)
  16. 图片怎么转化word文档?分享一个实用的方法。
  17. 移动WEB开发布局中的-box-sizing:border-box
  18. 建站选择Linux还是Windows操作系统?
  19. ASP.NET Web API实现简单的文件下载与上传
  20. ABAQUS中的文件类型及功能

热门文章

  1. python表达式计算器_Python正则表达式实现简易计算器功能示例
  2. mysql和mongodb配合_MongoDB和Mysql怎样结合
  3. java 接口参数验证_SpringBoot实现通用的接口参数校验
  4. 撕裂者cpu三代文件服务器,AMD三代线程撕裂者CPU开盖:钎焊散热、64核若隐若现...
  5. python三维数据转换成二维_Python科学计算全生态工具锦集
  6. Calabash探索3-Calabash进阶
  7. 4个开源在线调查工具
  8. XCTF联赛“出海计划”开启,八月新加坡站国际赛蓄势待发
  9. 我在车间写代码:我的代码能省1个亿
  10. PostgreSQL Oracle兼容性之 - plpgsql 自治事务(autonomous_transaction)补丁