gpt4 book ai didi

java - 进程在 waitFor() 中挂起

转载 作者:行者123 更新时间:2023-11-29 02:05:17 28 4
gpt4 key购买 nike

我正在尝试运行 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com