gpt4 book ai didi

java - Runtime.exec() 无输出

转载 作者:搜寻专家 更新时间:2023-11-01 03:28:03 25 4
gpt4 key购买 nike

运行 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”位于包 trafficcircle 中的\bin 下。我以为如果找不到 Runtime.exec 会首先提示...这是怎么回事?

最佳答案

我假设您遇到了一个您正在丢弃的错误。尝试使用 ProcessBuilder.redirectErrorStream(true);

当您尝试运行一个命令时,它不是在 shell 中运行的,并且可能会收到您在命令行中看不到的错误。我会明确地使用

"java","-classpath","bin","trafficcircle.Street","1","2"`

并确保您收到任何错误消息。

另一种选择是使用像这样的外壳

"/bin/bash", "-c", "java -classpath bin trafficcircle.Street 1 2"

关于java - Runtime.exec() 无输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8103097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com