java调用Oracle EXP剖析
来源:互联网 发布:房源发布软件 编辑:程序博客网 时间:2024/06/18 13:12
一眼看过去相信大家都知道用Runtime.getRuntime().exec来调用,我的需求就是:
调用Oracle EXP命令完成备份,并返回生成的备份文件名,这个备份文件会很快在其他的地方被使用。
采用Runtime.getRuntime().exec我们都知道,需要处理它的InputStream,以避免出现执行的命令输出的信息过多使得进程被堵死,OK,按照这样的方法,开写:
String[] cmds = new String[ 3 ];
cmds[ 0 ] = " cmd " ;
cmds[ 1 ] = " /C " ;
cmds[ 2 ] = "exp username/password@sid file=xxx.dmp log=xxx.log";
Process process = Runtime.getRuntime().exec(cmds);
try {
InputStreamReader isr = new InputStreamReader(process.getInputStream());
BufferedReader br = new BufferedReader(isr);
String line = null ;
while ((line = br.readLine()) != null ) {
System.out.println(line);
}
}
catch (IOException ioe) {
ioe.printStackTrace();
}
写完收工,执行,傻了,进程被挂S了,到底什么原因呢,开始瞎尝试,把读取process的InputStream的部分全部去掉,执行,竟然OK,更晕,但这个时候出现了一个问题,那就是没法知道什么时候备份文件完全生成了,如果在Runtime.getRuntime后去获取备份文件,那个时候甚至连备份文件都没生成,之后甚至尝试过轮询直到备份文件生成,再往下走,那样还是有问题,就是生成的那个备份文件永远都只有正常的一半的大小,只有在整个进程退出的时候它才正常的全部生成。cmds[ 0 ] = " cmd " ;
cmds[ 1 ] = " /C " ;
cmds[ 2 ] = "exp username/password@sid file=xxx.dmp log=xxx.log";
Process process = Runtime.getRuntime().exec(cmds);
try {
InputStreamReader isr = new InputStreamReader(process.getInputStream());
BufferedReader br = new BufferedReader(isr);
String line = null ;
while ((line = br.readLine()) != null ) {
System.out.println(line);
}
}
catch (IOException ioe) {
ioe.printStackTrace();
}
竟然碰到这样的问题,彻底晕,开始google,竟然只查到一篇和我这同样的问题,更郁闷的是那个帖子最后楼主写了一句“问题解决了”,但没写是怎么解决的,郁闷S。
只好自己开始尝试各种办法,上网抓人到处问,最后在自己不断的尝试下终于是出现了转机,在我的N+1次尝试的时候我改为只读取process的ErrorStream,然后执行,晕,OK了,在导出成功的情况下没有问题,但在导出有错误的时候(像sid不对,用户名错误,数据库没启动等)进程还是被挂S,但只要导出成功没问题,导出有错误的问题是很好处理的,OK,最后试验的代码改为这样:
String[] cmds = new String[3];
cmds[0] = "cmd";
cmds[1] = "/C";
cmds[2]=commandBuf.toString();
Process process=Runtime.getRuntime().exec(cmds);
boolean shouldClose=false;
try {
InputStreamReader isr = new InputStreamReader(process.getErrorStream());
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null){
if(line.indexOf("错误")!=-1){
shouldClose=true;
break;
}
}
}
catch (IOException ioe) {
shouldClose=true;
}
if(shouldClose)
process.destroy();
int exitVal = process.waitFor();
当然,实际的代码中不能像这里写的一样,直接去判断是否含错误这个字符串,而且这段代码是只适合在winnt版本以上的windows操作系统上执行的。cmds[0] = "cmd";
cmds[1] = "/C";
cmds[2]=commandBuf.toString();
Process process=Runtime.getRuntime().exec(cmds);
boolean shouldClose=false;
try {
InputStreamReader isr = new InputStreamReader(process.getErrorStream());
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null){
if(line.indexOf("错误")!=-1){
shouldClose=true;
break;
}
}
}
catch (IOException ioe) {
shouldClose=true;
}
if(shouldClose)
process.destroy();
int exitVal = process.waitFor();
这样反而可以了,说明什么呢,说明在执行oracle的exp时,出现了一个很怪的现象,就是exp在console输出的信息没有被放入InputStream,反而是放到了ErrorStream中(即使正确的情况也是),这就导致了按照正常的情况去写这段代码的话反而会出问题。
担心以后再次碰到这样的问题,备忘一下....
posted on 2006-11-22 22:43 BlueDavy 阅读(3301) 评论(8) 编辑 收藏 所属分类: Java
# re: java调用Oracle EXP备忘 2006-11-23 13:09 javabeginer
- java调用Oracle EXP剖析
- java 调用exp或imp
- Oracle exp
- Oracle-Exp
- java调用cmd命令 exp导出数据库表格数据
- Oracle imp/exp
- Oracleのexp & imp
- Oracle EXP && IMP
- ORACLE imp&exp使用
- oracle exp命令
- Oracle Imp exp使用方法
- exp,imp(oracle)
- oracle exp 命令
- ORACLE EXP 的使用
- oracle exp/imp 详解
- oracle exp 使用
- oracle exp imp
- oracle imp/exp
- setXORMode的用法 (转载)
- applet 加载提示
- 自动导入RSA密钥容器
- 关于String类的split方法解析
- java实用知识集
- java调用Oracle EXP剖析
- Java自动设置环境变量
- 结构模式之Bridge
- 结构模式之Decorator(油漆工)
- 结构模式之Composite(组合)
- 结构模式之Proxy(代理)
- 创建模式之Singleton(单态)
- 创建模式之Builder (建设)
- 创建模式之Prototype(原型)
评论
从没写过有关process的东西,学习! 回复 更多评论
# re: java调用Oracle EXP备忘 2006-11-24 14:38 kruce[匿名]
正如你所说的,处理Process的时候要注意及时读取子进程的输出流,不然可能导致子进程堵塞,甚至死锁。
如果我是你,我会这么来排除bug。
1,把对InputStream的处理放到一个单独Thread里面。
2,用ProcessBuilder的redirectErrorStream来合并OutputStream和ErrorStream。注意子进程的InputStream对应父进程的OutStream。如果不合并这两个流的话则必须并行排空它们,顺序的排空会导致思索。
建议你好好看看5.0的API和JLS。 回复 更多评论
# re: java调用Oracle EXP备忘 2006-11-24 21:34 BlueDavy
@kruce[匿名]
....我用的是1.4....虽然我也很想用5.0的ProcessBuilder 回复 更多评论
# re: java调用Oracle EXP备忘 2007-01-11 16:41 roygbip
我也遇到你这样的问题,不过我还是用getInputStream()才可以,用getErrorStream我也试了一下,只能倒入300条数据就卡住了. 回复 更多评论
# re: java调用Oracle EXP备忘 2007-04-26 17:10 lq
我今天遇到这个问题了 回复 更多评论
# re: java调用Oracle EXP备忘 2007-05-28 16:56 lift
Win的控制台程序和UNIX下的命令执行完毕后,都会有一个出口值(EXIT CODES),如果执行成
功的话,这个值一般为0,不成功则为其它值。在Windows下,可以在程序执行完毕后,马上输入
echo %ERRORLEVEL%查看这个值;在Unix下则是输入 echo $?。实质上都是保存在一个环境变量
中。
在Java中通过Process来运行外部程序的时候,可以调用Process的exitValue()方法来获得程序
的出口值。Oracle的exp和imp在各种情况下的出口值如下:
A. Successful export/import.
-------------------------
Result : Export terminated successfully without warnings
Import terminated successfully without warnings
Exit Code : EX_SUCC
Exit Level: 0
B. Successful export/import with warnings.
---------------------------------------
Result : Export terminated successfully with warnings
Import terminated successfully with warnings
Exit Code : EX_OKWARN
Exit Level: 0 (for Unix platforms)
1 (for Windows platform with Oracle9i, Oracle8i, and below)
3 (for Windows platform with Oracle10g and higher)
C. Unsuccessful export/import.
---------------------------
Result : Export terminated unsuccessfully
Import terminated unsuccessfully
Exit Code : EX_FAIL
Exit Level: 1 (for Unix platforms, and for Windows platforms with
Oracle10g and higher)
3 (for Windows platform with Oracle9i, Oracle8i, and below)
回复 更多评论
# re: java调用Oracle EXP备忘 2007-05-28 17:07 lift
exp和imp的输出是要从ErrorStream中获取,这是我以前写的
Process proc = null;
try
{
proc = Runtime.getRuntime().exec(cmd.toString());
InputStream istr = proc.getErrorStream();
BufferedReader br = new BufferedReader(new InputStreamReader(istr));
String str;
while ((str=br.readLine()) != null)
{
errorInfo.append(str + "/n");
}
proc.waitFor();
}
catch (Exception e)
{
...
}
if (proc.exitValue() == 0)
{
proc.destroy();
return true;
}
else
{
if(logger.isDebugEnabled())
logger.debug(errorInfo);
proc.destroy();
return false;
}
引用与:
好牛这些人
http://www.blogjava.net/BlueDavy/archive/2006/11/22/82909.html