简体   繁体   English

Runtime.exec()没有输出

[英]Runtime.exec() no output

Code that runs 4 'Street' processes: 运行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);
    }

Where 'Street' is: “街道”在哪里:

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();
}

} }

Prints out: 打印输出:

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

'Street.class' in my Eclipse project is under \\bin in package trafficcircle. 我的Eclipse项目中的“ Street.class”位于包trafficcircle中的\\ bin下。 I thought Runtime.exec would complain first if it wasn't found...what's up with this? 我以为如果没有找到Runtime.exec,它会首先抱怨...这是怎么回事?

I assume you are getting an error which you are discarding. 我认为您遇到了要丢弃的错误。 Try using ProcessBuilder.redirectErrorStream(true); 尝试使用ProcessBuilder.redirectErrorStream(true);

When you try to run a command it is not run in a shell, and may be getting an error which you don't see on the command line. 当您尝试运行命令时,该命令未在外壳中运行,并且可能会收到您在命令行中未看到的错误。 I would explicitly use 我会明确使用

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

and make sure you are getting any error messages. 并确保您收到任何错误消息。

another option is to use a shell like 另一种选择是使用像

"/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