首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Runtime.exec()无输出

Runtime.exec()无输出
EN

Stack Overflow用户
提问于 2011-11-12 15:00:20
回答 2查看 1.7K关注 0票数 4

运行4个“街道”进程的代码:

代码语言:javascript
复制
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);
    }

其中‘街道’是:

代码语言:javascript
复制
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();
}

}

打印输出:

代码语言:javascript
复制
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会先抱怨...这是怎么回事?

EN

回答 2

Stack Overflow用户

发布于 2011-11-12 17:19:43

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

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

代码语言:javascript
复制
"java","-classpath","bin","trafficcircle.Street","1","2"`

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

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

代码语言:javascript
复制
"/bin/bash", "-c", "java -classpath bin trafficcircle.Street 1 2"
票数 2
EN

Stack Overflow用户

发布于 2011-11-13 06:28:03

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8103097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档