繁体   English   中英

Runtime.exec()没有输出

[英]Runtime.exec() no output

运行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);

当您尝试运行命令时,该命令未在外壳中运行,并且可能会收到您在命令行中未看到的错误。 我会明确使用

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

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

另一种选择是使用像

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

使用./bin(带点)来使用相对路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM