从Java流程执行时,跳过批处理文件中的暂停命令

黑色

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

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

这是我的test.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并发送随机密钥全部无济于事。

有任何想法吗?

FlexEast

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

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

运行时安全修改批处理文件

忽略批处理文件中以#开头的处理行

如何验证批处理文件中是否存在文件?

R中的批处理文件中的setwd()

在批处理文件网址中添加加号

在批处理文件中定义和使用变量

在Powershell中设置批处理文件的变量

在Windows批处理文件中的SET上使用变量

批处理文件中的字符串替换问题

从Windows批处理文件调用SUBST

从批处理文件运行Powershell命令以重命名网络驱动器

如何通过批处理文件中的时间戳识别/获取文件?

批处理文件以计数子目录中的文件类型

批处理文件以从txt文件中删除前18个字符

批处理文件中的空白,并保存在其他目录中

如何将大于和小于添加到批处理文件变量中

替换批处理文件变量进给ffmpeg程序中的特殊字符

批处理文件以删除超过N天的文件

将2个批处理文件合并为1个,但使它们在2个命令窗口中运行

无法将webpack识别为内部或外部命令,可操作程序或批处理文件

无法将“ adb”识别为内部或外部命令,可操作程序或批处理文件

无法将“ python3”识别为内部或外部命令,可操作程序或批处理文件

尽管从命令行调用该批处理文件正常工作,但为什么从快捷方式调用该批处理文件却会立即关闭呢?

如何在批处理文件中提示用户输入

运行多个批处理文件,但有例外

批处理文件,将所有文件内容读入单个文件

Windows批处理文件从定界符之间没有数据的文件读取

从批处理文件调用脚本时如何指定开关参数

使用Python运行批处理文件时出现问题