java - 进程卡在 waitFor() 中

标签 java mysql process

我正在尝试运行mysql来执行java中的一些文件。 输入是从文件中读取的,应该通过管道传输到 mysql 进程中,一切看起来都很好,但是行

int exitCode = proc.waitFor(); 

一去不复返。

private boolean runScript(String path, String cmd, File file) throws IOException, InterruptedException {
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec( path + File.separatorChar + cmd );
        OutputStream procOS = proc.getOutputStream();
        InputStream procES = proc.getErrorStream();
        InputStream procIS = proc.getInputStream();
        OutputStreamWriter procStdIn = new OutputStreamWriter( procOS );

        FileInputStream fis = new FileInputStream( file );
        BufferedReader reader = new BufferedReader( new InputStreamReader( fis ) );
        String send;

        while ( ( send = reader.readLine() ) != null ) {
            procStdIn.write( send );
            System.out.println( send );
            copyStream( procES, System.err );
            copyStream( procIS, System.out );
        }
        procStdIn.write( "\r\nexit\r\n" );
        int exitCode = proc.waitFor();
        return exitCode == 0;
    }

    private void copyStream(InputStream is, PrintStream err) throws IOException {
        byte b[] = new byte[ 1024 ];
        int length;
        while ( is.available() > 0 && ( length = is.read( b ) ) > 0 ) {
            err.write( b, 0, length );
        }

}

最佳答案

我怀疑您正在阻止读取 stdout 和 stderr。请参阅this answer了解更多详情。

关于java - 进程卡在 waitFor() 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7388145/

相关文章:

java - Runnable 是如何从 Java8 lambda 创建的

java - jackson xml 模块 : deserializing immutable type with a @JacksonXmlText property

php - MySQL查询-如果不存在-插入-否则-更新

java - 从在 java 中执行的 shellscript 运行时 ffmpeg 中的段错误

java - 从 Java 运行 MySql 失败

powershell - 我正在尝试获取在远程计算机上运行的进程已加载的模块

java - URL 中的上次修改日期

java - 将 lzo 文件作为数据集导入 java Spark

php - wkhtmltopdf 从文件随机空框生成 PDF

sql - 使用 GROUP BY/HAVING 重构子查询?