当ProcessBuilder使用输入提示符

时间:2018-04-24 23:43:37

标签: java processbuilder

我有一个执行MYSQL命令的批处理和shell脚本。

mysql -u user -p < path\etc\script\db\dosomething.sql

我有一个使用ProcessBuilder执行批处理脚本的java程序。 我知道这个命令会提示用户输入密码,我也知道我可以使用--password(抛出不安全警告)或配置文件来设置登录参数。 我想通过ProcessBuilder使用输入提示提供密码,但是当我运行程序时它只是挂起而我不知道为什么。 当我使用JVisualVM查看正在发生的事情时,我注意到OutputStream线程没有运行,如果它运行了,那么挂起程序的是什么?以下是我的实施: -

处理InputStream和ErrorStream的类:

public static class InStream extends Thread {

    private final InputStream in;
    private final PrintWriter pw;

    public InStream(InputStream in, PrintWriter pw, String threadName) {
        super(threadName);
        this.in = in;
        this.pw = pw;
    }

    @Override
    public void run() {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(in));) {
            String line;
            while ((line = br.readLine()) != null) {
                if (Thread.currentThread().isInterrupted()) {
                    Thread.currentThread().interrupt();
                    break;
                }
                pw.println(line);
                pw.flush();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}

处理OutputStream的类:

public static class OutStream extends Thread {

    private final String message;
    private final OutputStream out;

    public OutStream(OutputStream out, String threadName, String message) {
        super(threadName);
        this.out = out;
        this.message = message;
    }

    @Override
    public void run() {
        try (PrintWriter pw = new PrintWriter(out);) {
            pw.println(message);
            pw.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

这是主程序:

public static void main(String[] args) {
    try {
        String password = "Y0urp@ssw0rd";
        ProcessBuilder builder = new ProcessBuilder("path\\etc\\script\\cli\\batchWithSQLCommand.bat");
        Process process = builder.start();
        StringWriter errors = new StringWriter();
        StringWriter input = new StringWriter();
        new OutStream(process.getOutputStream(), "output-stream-thread", password).start();
        new InStream(process.getErrorStream(), new PrintWriter(errors, true), "error-stream-thread").start();
        new InStream(process.getInputStream(), new PrintWriter(input, true), "input-stream-thread").start();
        process.waitFor();
        System.out.println("inputStream: " + input.toString());
        System.out.println("Error Stream: " + errors.toString());
    } catch (IOException | InterruptedException ex) {
       //throw error here
    } catch (Error | Exception ex) {
         //throw error here
    }
}

可能是什么问题?

0 个答案:

没有答案