首页 > 学院 > 开发设计 > 正文

Java程序中如何执行其他的程序

2019-11-18 14:22:32
字体:
来源:转载
供稿:网友

  java 程序中如何执行其他的程序?使用了 exec() 之後, 要如何才能使用 I/O redirection?


以Unix 为例, DOS 或 Window 系统相似:
import java.io.*;
import java.util.*;

class IoRedirect {
public static void main(String Argv[])
{
try
{
String[] command = {"/bin/sh", "-c", "/bin/ls > out.dat"};
PRocess p = Runtime.getRuntime().exec(command);
p.waitFor();
System.out.println("return code: " + p.exitValue());
}
catch (IOException e)
{
System.err.println("IO error: " + e);
}
catch (InterruptedException e1)
{
System.err.println("Exception: " + e1.getMessage());
}
}
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表