运行4个“街道”进程的代码:
for (int i=0; i < NUM_STREETS; i++) {
Process process = runtime.exec("java -classpath \\bin trafficcircle.Street 1 2");
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null && !line.isEmpty()) {
System.out.println(line);
System.out.flush();
}
InputStream es = process.getErrorStream();
InputStreamReader esr = new InputStreamReader(es);
BufferedReader br2 = new BufferedReader(esr);
while ((line = br2.readLine()) != null && !line.isEmpty()) {
System.out.println(line);
System.out.flush();
}
int exitVal = process.waitFor();
System.out.println("Process exitValue: " + exitVal);
}其中‘街道’是:
public class Street {
/**
* @param args
* 0 - Simulation run time
* 1 - Flow time interval
*/
public static void main(String[] args) {
System.out.println(args[0]);
System.out.println(args[1]);
System.out.flush();
}}
打印输出:
Error: Could not find or load main class trafficcircle.Street
Process exitValue: 1
Error: Could not find or load main class trafficcircle.Street
Process exitValue: 1
Error: Could not find or load main class trafficcircle.Street
Process exitValue: 1
Error: Could not find or load main class trafficcircle.Street
Process exitValue: 1我的Eclipse项目中的'Street.class‘位于\bin包的trafficcircle下。我以为如果找不到的话Runtime.exec会先抱怨...这是怎么回事?
发布于 2011-11-12 17:19:43
我假设你得到了一个你正在丢弃的错误。尝试使用ProcessBuilder.redirectErrorStream(true);
当您尝试运行一个命令时,它不是在shell中运行的,并且可能会得到一个您在命令行中看不到的错误。我会显式地使用
"java","-classpath","bin","trafficcircle.Street","1","2"`并确保您收到任何错误消息。
另一种选择是使用像这样的shell
"/bin/bash", "-c", "java -classpath bin trafficcircle.Street 1 2"发布于 2011-11-13 06:28:03
使用./bin (带点)来使用相对路径。
https://stackoverflow.com/questions/8103097
复制相似问题