Java读取Oracle EXP命令输出
我需要通过 Java 程序运行 Oracle EXP 命令并在某处打印命令输出。
EXP 命令是正确的,当我执行 Java 代码时,转储文件已正确创建,但我在获取输出时遇到了一些问题。
这是一个与我用来读取输出的代码片段非常相似的代码片段:
String line;
String output = "";
try {
Process p = Runtime.getRuntime().exec(myCommand);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
output += (line + '\n');
}
input.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
System.out.println(output);
正如我所说,该命令已正确执行(通过生成的转储文件进行验证),但我的控制台上没有出现任何内容,并且我的 Java 程序不会终止任何一个。
如果我使用另一个命令,如“ls -l”而不是“exp ...”,相同的代码可以完美工作。
I need to run the Oracle EXP command through a Java program and print somewhere the command output.
The EXP command is correct, the dump file is created correctly when I execute my Java code, but I'm experiencing some issues to get the output.
This is an snippet very similar to the one I'm using to read the output:
String line;
String output = "";
try {
Process p = Runtime.getRuntime().exec(myCommand);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
output += (line + '\n');
}
input.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
System.out.println(output);
As I said, the command is correctly executed (verified through the generated dump file), but nothing appears on my console and my Java programs doesn't terminate either.
The same code works perfectly if I use another command, as "ls -l" instead of "exp ...".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许 exp 正在写入标准错误输出而不是标准输出。
尝试使用
p.getErrorStream()
而不是getInputStream()
Maybe exp is writing to standard error output rather than standard output.
Try to use
p.getErrorStream()
instead ofgetInputStream()
正如 a_horse_with_no_name 所说,可能是错误流缓冲区已满,因此阻塞了程序执行。
尝试启动一个
Thread
来读取错误流,或者使用ProcessBuilder
类将错误流重定向到stdout(您已经读取过)。As a_horse_with_no_name said, it might be that the error stream buffer is full and thus is blocking the programm execution.
Either try to start a
Thread
to also read the error stream or use theProcessBuilder
class to redirect the error stream to stdout (which you already read).