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

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

我正在尝试编写一个Java应用程序,它将调用一个批处理文件,打印出批处理文件正在执行的操作,并等待它完成执行,然后再执行其他操作。我已经读到,发送一个LIVEFION进程。GETOutPuthStand()会触发“按任意键继续…”提示符,但它不是为我做的。

这是我的测试.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和发送随机密钥,但都没有成功。

有什么主意吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值