1

我正在尝试通过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);
4

1 回答 1

0

\\r不是插入符号返回,它\后面是r. 插入符号返回只是\r

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

于 2013-03-06T12:26:37.843 回答