从Java运行导入数据库DOS命令

时间:2013-04-10 13:36:10

标签: java import dos

我正在尝试从这样的Java程序运行数据库导入命令:

public class Test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String[] str = {"imp ASKUL/askul@ASKDB file=askdbinstall.dmp log=askul.log fromuser=askul touser=ASKUL full=N ignore=Y grants=Y indexes=Y;"};
        Process pro;
        try {
            pro = Runtime.getRuntime().exec(str);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

错误输出为:

java.io.IOException: Cannot run program "imp ASKUL/askul@ASKDB file=askdbinstall.dmp log=askul.log fromuser=askul touser=ASKUL full=N ignore=Y grants=Y indexes=Y;": CreateProcess error=2, The system cannot find the file specified

文件askdbinstall.dmp存在,因为如果我在CMD中贴出相同的命令,它正在导入数据库转储很好。什么是我的错误?

添加了: 从Reimius建议我也试过这个:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
public class Tes {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
      try {
          String [] cmd = {"imp", "ASKUL/askul@ASKDB file=askdbinstall.dmp", 
                "log=askul.log", "fromuser=askul", "touser=ASKUL", 
                "full=N ignore=Y grants=Y indexes=Y;"};
     Process process = Runtime.getRuntime().exec(cmd);
     InputStream in = process.getInputStream();
     InputStreamReader ins=new InputStreamReader(in);
     BufferedReader br = new BufferedReader(ins);
     String data = null;
     while ((data = br.readLine()) != null) {
        System.out.println(data);
     }
   } catch (Exception e) {
    System.out.println(e);
   }
   }
} 

输出

run:
BUILD SUCCESSFUL (total time: 3 seconds)

没有进口。

1 个答案:

答案 0 :(得分:1)

您的导入命令String被视为一个命令。尝试分解令牌。还要检查Process#getErrorStream输出的内容:

String[] str = {"imp", "ASKUL/askul@ASKDB file=askdbinstall.dmp", 
                "log=askul.log", "fromuser=askul", "touser=ASKUL", 
                "full=N ignore=Y grants=Y indexes=Y;"};

process = Runtime.getRuntime().exec(str);
BufferedReader in = 
           new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line = null;
while ((line = in.readLine()) != null) {
    System.err.println(line);
}

除此之外:ProcessBuilder可以更轻松地使用参数传递。