我需要创建一个java实用程序,以便我们团队中的一个进程自动化。现有进程调用可执行文件并按顺序进行业务处理。我为此写了一个小程序
FileInputStream fstream;
try {
String [] cmd = new String[3];
if(System.getProperty("os.name").toLowerCase().contains("win")){
cmd[0] = "cmd.exe";
cmd[1] = "/C";
} else{
cmd[0] = "/bin/sh";
cmd[1] = "-c";
}
fstream = new FileInputStream("C:\\JavaProgram.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String FileLine,FileLine1;
//Read File Line By Line
while ((FileLine = br.readLine()) != null)
{
cmd[2]=FileLine;
ProcessBuilder proc = new ProcessBuilder(cmd);
Process p = null;
proc.redirectErrorStream(true);
System.out.println("Starting Process "+ FileLine);
p = proc.start();
BufferedReader br1 = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((FileLine1 = br1.readLine()) != null)
{
System.out.println("Output"+ FileLine1);
}
int exitValue = p.waitFor();
System.out.println("\nExit Value is " + exitValue);
}
br.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
程序读取JavaProgram文件,该文件包含要按顺序执行的程序列表。现在我有以下问题:
waitfor()
输出。当前逻辑只显示了值[我稍后会增强以显示正确的错误消息]