簡體   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