本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告

戒除浮躁,读好书,交益友


我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:邢红瑞的blog
日志总数:523
评论数量:1142
留言数量:0
访问次数:9698018
建立时间:2004年12月20日




[java语言]执行的Runtime类调用程序停掉的原因
文章收藏,  软件技术

邢红瑞 发表于 2005/11/6 14:40:39

牛牛发现问题的原因,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()方法可以在java程序里运行外部程序.     该方法有6个可访问版本:     1.exec(String   command)     2.exec(String   command,   String   envp[],   File   dir)       3.exec(String   cmd,   String   envp[])     4.exec(String   cmdarray[])     5.exec(String   cmdarray[],   String   envp[])     6.exec(String   cmdarray[],   String   envp[],   File   dir)         一般的应用程序可以直接使用第一版本,当有环境变量传递的时候使用后面的版本.     其中2和6版本可以传递一个目录,标识当前目录,因为有些程序是使用相对目录的,所以就要使用这个版本.     当要执行批处理的时候,不能直接传递批处理的文件名,而要使用:     cmd.exe   /C   start   批处理文件名     使用dos命令(比如dir)时也要使用掉调用.         如果想与调用的程序进行交互,那么就要使用该方法的返回对象Process了,通过Process的getInputStream(),getOutputStream(),getErrorStream()方法可以得到输入输出流,然后通过InputStream可以得到程序对控制台的输出信息,通过OutputStream可以给程序输入指令,这样就达到了程序的交换功能.     例子如下:     package   com.xxx.netadmin.schedule;     import   java.io.PrintWriter;     import   java.io.PrintStream;     import   java.io.IOException;     import   java.sql.SQLException;     import   java.util.Date;     import   java.io.StringReader;     import   java.io.BufferedReader;     import   java.io.InputStreamReader;     import   java.io.Reader;     import   java.io.File;     import   java.io.BufferedWriter;     import   java.io.OutputStreamWriter;       public   class   ExecuteTask   implements   Runnable   {         private   boolean   isRunning=true;         public   ExecuteTask()   {         }         public   void   run(){         }         public   static   void   main(String[]   args){             try   {       Process   proc=Runtime.getRuntime().exec("cmd.exe");                     BufferedReader   read=new   BufferedReader(new   InputStreamReader(proc.getInputStream()));                 new   Thread(new   Echo(read)).start();                 PrintWriter   out=new   PrintWriter(new   OutputStreamWriter(proc.getOutputStream()));                 BufferedReader   in=new   BufferedReader(new   InputStreamReader(System.in));                 String   instr   =   in.readLine();                 while(!"exit".equals(instr)){                     instr   =   in.readLine();                     out.println(instr);                     file://out.println("telnet   192.168.0.1");                     out.flush();                 }                 in.readLine();                 read.close();                 out.close();             }             catch   (IOException   ex)   {                 ex.printStackTrace();             }         }     }     class   Echo   implements   Runnable   {         private   BufferedReader   read;         public   Echo(BufferedReader   read){             this.read   =   read;         }         public   void   run()   {             try   {                 String   l   =   read.readLine();                 while   (l   !=   null)   {                     System.out.println(l);                     l   =   read.readLine();                 }                 System.out.println("---执行完毕:");             }             catch   (IOException   ex)   {                 ex.printStackTrace();             }         }     }Linux下public static void execute(String cmd) throws Exception{    LogHelper.getLogger().info("邮件过滤处理进程cmd-"+cmd);    Process process=null;    BufferedReader br = null;    String s=null;    try    {        process=Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",cmd});        br = new BufferedReader(new InputStreamReader(process.getInputStream()));        while( (s = br.readLine())!=null )        {            LogHelper.getLogger().error("邮件迁移进程处理----"+s);        }    }        finally    {        if( process!=null )        {            process.destroy();            }        process=null;        if( br!=null )        {            try            {                br.close();                }            catch(Exception e)            {            }            br = null;        }            } } 本程序和思路 ,没有用于启明星辰的任何设备和项目,大家放心使用。


阅读全文(5814) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.324 second(s), page refreshed 144775540 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号