进程挂在 waitFor() 中

发布于 2024-12-04 15:40:24 字数 1433 浏览 0 评论 0原文

我正在尝试运行 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 );
        }

}

I am trying to run mysql to execute some files from java.
The input is read from a file and should be piped into the mysql process, everthing seems ok but the line

int exitCode = proc.waitFor(); 

never returns.

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 );
        }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

能怎样 2024-12-11 15:40:24

我怀疑你正在阻止读取 stdout 和 stderr。有关更多详细信息,请参阅此答案

I suspect you're blocking reading stdout and stderr. See this answer for more details.

浮云落日 2024-12-11 15:40:24

添加:

procStdIn.flush();

之后:

procStdIn.write( "\r\nexit\r\n" );

Add:

procStdIn.flush();

after:

procStdIn.write( "\r\nexit\r\n" );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文