【发布时间】:2011-05-12 03:58:20
【问题描述】:
我是 Java 新手。我的任务是编写 java 程序来运行命令行。我在 DOS 提示符下测试了命令行,因为我还没有访问 Linux 机器的权限。它工作得很好。有关完整的命令行语法,请参见下面的程序。该作业将采用 6 个输入文件并生成一些输出文件。接下来,我尝试创建一个类并使用 getruntime 和 process 来处理这项工作。即使它编译没有错误但是当我运行它时只是显示光标闪烁......我想我需要使用线程异步技术。请提供一些建议,因为我没有足够的时间来做这些项目。我还想在工作完成后实现回调或返回值。一个例子将不胜感激。谢谢
import java.io.*;
public class RunJob {
// public static final String PROGRAM = "c:\\wrk\\java.exe Hello";
//one command line below
public static final String PROGRAM = "c:/java.exe -cp \"wrk/jmp.jar;wrk/colt.jar\" gov.lanl.yadas.reliability.UltimateMissileReliabilityModel 10000 \"wrk/\" x1.dat x2c.dat x3.dat x4.dat x5.dat x6.dat true";
// Set to true to end the loop
static boolean done = false;
public static void main(String argv[]) throws IOException {
BufferedReader is;
String line;
String returnMsg = "Start ";
final Process p = Runtime.getRuntime().exec(PROGRAM);
System.out.println("start");
Thread waiter = new Thread() {
public void run() {
try {
p.waitFor();
} catch (InterruptedException ex) {
System.out.println("InterruptedException");
return;
}
System.out.println("Program terminated!");
done = true;
}
};
waiter.start();
is = new BufferedReader(new InputStreamReader(p.getInputStream()));
while (!done && ((line = is.readLine()) != null))
{
System.out.println(line);
returnMsg = returnMsg + line;
}
System.out.println(returnMsg);
System.out.println("End");
return;
}// main
}
【问题讨论】:
标签: java