- Execute an external program and capture the output

博客涉及JDK相关内容,包含output(输出)、exception(异常)、string(字符串)、path(路径)等方面。可能围绕JDK在处理输出、异常时的情况,以及路径和字符串操作等信息技术要点展开。

Execute an external program and capture the output


 import java.io.*;
 public class CmdExec {
   public CmdExec(String cmdline) {
     try {
      String line;
      Process p = Runtime.getRuntime().exec(cmdline);
      // jdk1.0.2
      DataInputStream input = 
        new DataInputStream(p.getInputStream());
      // jdk1.1.1
      // BufferedReader commandResult = 
      //  new BufferedReader
      //    (new InputStreamReader(p.getInputStream()));
      while ((line = input.readLine()) != null) {
        System.out.println(line);
        }
      input.close();
      } 
     catch (Exception err) {
      System.out.println("EXEC failed: " + err.toString());
      err.printStackTrace();
      }
    }

 public static void main(String argv[]) {
   /*
   **  javac CmdExec tree.com
   **  javac CmdExec "tree.com /a"
   */
   new CmdExec(argv[0]);
   }
 }

You can include a path for the program to be executed. On the Win plateform, you will have
problem if the path contains spaces in it. To fix the problem, put the path in quotes.


  public class Test {
    public static void main(String[] args) throws Exception {
      Process p = Runtime.getRuntime().exec(
        "/"c:/program files/windows/notepad.exe/"");
         p.waitFor();
      }
   }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值