javaからいくつかのファイルを実行するためにmysqlを実行しようとしています。入力がファイルから読み込まれるとmysqlのプロセスにパイプする必要があります 、everthingは大丈夫そうですが、ラインwaitFor()でプロセスがハングアップ
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);
}
}
あなたは子プロセスが終了したことを参照していますか?おそらく 'copyStream'は最後に' exit'を書いた後に実行する必要がありますか? –
@Hemalまだ、処理プロセスが実行されていません。 – stacker