通过ProcessBuilder执行的'C'二进制文件阻止了执行流程

时间:2013-03-06 06:23:14

标签: java bufferedreader processbuilder bufferedwriter

我正在尝试通过Java执行二进制文件(用'C'编写),而二进制文件成功执行。在按下“Enter”键之前,它不会将执行控制返回到此代码(被阻止)。由于此问题,'prcs.waitfor()== 0'永远不会执行,用户也不知道二进制执行是否成功。我试图在OutputStream上创建BufferedWriter来发送'Enter'键击(// r),但它不起作用。这里需要做什么,以便执行控制回到此代码并执行'prcs.waitfor()== 0'。我需要继续执行另一个依赖于第一个命令成功执行的命令。我坚持这个:(

    // Start ProcessBuilder, 'str' contains a command

    ProcessBuilder pbuilder = new ProcessBuilder(str);
    pbuilder.directory(new File("/root/workspace/Project1"));
    pbuilder.redirectErrorStream(true);
    Process prcs = pbuilder.start();
    AForm.execStatustext.append("\n=> Process is:" + prcs);

    // Read output
    StringBuilder out = new StringBuilder();
    BufferedReader bfrd = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String current_line = null, previous_line = null;
    while ((current_line = bfrd.readLine()) != null) {
        if (!line.equals(previous_line)) {
            previous_line = current_line;
            out.append(current_line).append('\n');
            //System.out.println(line);
        }
    }
    //process.getInputStream().close();
    // Send 'Enter' keystroke through BufferedWriter to get control back
    BufferedWriter bfrout = new BufferedWriter(new OutputStreamWriter(prcs.getOutputStream()));
    bfrout.write("\\r");
    bfrout.newLine();
    bfrout.flush();
    bfrout.write("\\r");
    bfrout.newLine();
    bfrout.flush();
    //process.getOutputStream().close();*/

    if (prcs.waitFor() == 0)
        System.out.println("Commands executed successfully");
    System.exit(0);

1 个答案:

答案 0 :(得分:0)

\\r不是插入符号,\后跟r。插入符号返回仅为\r

另外,bfrout.newLine();应该涵盖与平台相关的行分隔符。也许你根本不需要插入符号?