java - 从 Java 进程执行时跳过批处理文件中的暂停命令

标签 java batch-file process pause

抱歉,如果这是重复的,但没有其他解决方案有效。

我正在尝试编写一个Java应用程序,它将调用批处理文件,打印出批处理文件正在执行的操作,并等待它完成执行,然后执行其他操作。我读过通过 Process.getOutputStream() 发送换行会触发“按任意键继续...”提示,但它不适合我。

这是我的测试.bat:

@echo off
echo This is a test batch file.
echo This text should be readable in the console window
echo Pausing...
pause
echo done.
exit

这是我的 Java 驱动程序:

public class Tester {

    String cmd[] = { "cmd", "/c", "C:/Test Folder/test.bat" };

    BufferedReader in = null;
    BufferedWriter out = null;
    Process p = null;

    public Tester() {
        System.out.println( "Starting process..." );
        ProcessBuilder pb = new ProcessBuilder( cmd ).redirectErrorStream( true );
        try {
            p = pb.start();
            System.out.println( "Started process " + p.hashCode() );
            in = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
            out = new BufferedWriter( new PrintWriter( p.getOutputStream() ) );
            String line = "";
            while ( ( line = in.readLine() ) != null ) {
                System.out.println( "INFO [" + p.hashCode() + "] > " + line );

                // Wait for "Press any key to continue . . . from batch"

                if ( line.startsWith( "Press" ) ) {
                    System.out.println( "INFO [ SYS ] > Script may have called PAUSE" );
                    out.write( "\n\r" );
                }
            }
            int e = p.waitFor();
            if ( p.exitValue() != 0 )
                System.err.println( "Process did not finish successfully with code " + e );
            else
                System.out.println( "Process finished successfully with code " + e );
        } catch ( IOException ioe ) {
            System.err.println( "I/O: " + ioe.getMessage() );
        } catch ( InterruptedException ie ) {
            System.err.println( "Interrupted: " + ie.getMessage() );
        } catch ( Exception e ) {
            System.err.println( "General Exception: " + e.getMessage() );
        } finally {
            try {
                in.close();
                out.close();
                p.destroy();
            } catch ( Exception e ) {
                System.err.println( "Error closing streams: " + e.getMessage() );
            }
        }
        System.out.println( "Tester has completed" );
    }
    public static void main( String[] args ) {
        new Tester();
    }
}

在while循环中,会打印出:

Starting process...
Started process 2018699554
INFO [2018699554] > This is a test batch file.
INFO [2018699554] > This text should be readable in the console window
INFO [2018699554] > Pausing...

并且永远不要传递暂停语句。我尝试过重定向错误输出、发送 VK_ENTER 和发送随 secret 钥,但均无济于事。

有什么想法吗?

最佳答案

对于缓冲写入器,您必须在实际写入之前刷新缓冲区。在out.write()之后调用out.flush()。

关于java - 从 Java 进程执行时跳过批处理文件中的暂停命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50935104/

相关文章:

java - Java中类型删除的异常(exception)是什么?

batch-file - 通过拖放到批处理文件上运行命令

batch-file - 如何在 Windows 中使用批处理脚本创建符号链接(symbolic link)?

iphone - UI优先还是逻辑优先?

java - 将字符串列表作为 Cucumber 参数传递

java - 在 Java 中对 Shift-JIS 字符使用 XOR

batch-file - Windows Batch : How to append the current date and time of a format (yyyymmdd. hhmmss) 在文件名中?

android - 在服务中处理 KILL 信号?

python 队列并发进程管理

java - Safari 浏览器中的引荐来源网址 - 不可用?