如何使用java响应命令行

如何使用java响应命令行,java,apache,command-line,command,command-line-arguments,Java,Apache,Command Line,Command,Command Line Arguments,我正在使用java代码执行命令。但一旦命令开始运行,它就会请求一些用户输入。我不知道如何使用java代码提供输入。请帮帮我 我正在使用apacheorg.apache.commons.exec.CommandLine包来运行该命令 使用java代码运行命令后输出: [java] The ear file could archive up to: 17 MB in size. [java] Are you sure you want to build it? [y]Ear buildi

我正在使用java代码执行命令。但一旦命令开始运行,它就会请求一些用户输入。我不知道如何使用java代码提供输入。请帮帮我

我正在使用apacheorg.apache.commons.exec.CommandLine包来运行该命令

使用java代码运行命令后输出:

   [java] The ear file could archive up to: 17 MB in size.
   [java] Are you sure you want to build it? [y]Ear building was cancelled by the user
Java代码:

try {
            CommandLine cmdLine = new CommandLine(command);
            for (int i = 0; i < args.size(); i++) {
                cmdLine.addArgument(args.get(i));

            }

            DefaultExecutor exec = new DefaultExecutor();
            exec.setWorkingDirectory(new File(dir));
            if (timeout > 0) {
                ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
                exec.setWatchdog(watchdog);
            }
                exitValue = exec.execute(cmdLine);
            return exitValue;
        } catch (Exception e) {
            log.error(e, e);
        }
试试看{
命令行cmdLine=新命令行(命令);
对于(int i=0;i0){
ExecuteWatchdog看门狗=新的ExecuteWatchdog(超时);
执行setWatchdog(看门狗);
}
exitValue=exec.execute(cmdLine);
返回现有值;
}捕获(例外e){
日志错误(e,e);
}

使用java中的system.in接受输入值

最好使用(..)将对象返回给您


进程具有连接到子进程的正常输入的getOutputStream()方法。在脚本完成时使用和检测有点棘手,但给了您更多的自由。

但是在代码中的“您确定要构建它吗?[y]”之后,我如何才能提供输入。syso(您确定吗?[y][n])使用bufferreader。下面是将有帮助的链接