0

我正在尝试从这样的 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 中粘贴相同的命令,它正在导入数据库 Dump 很好。我的错误是什么?

补充:从 Reimius Suggestion 我也试过这个:

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)

没有进行导入。

4

1 回答 1

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使参数传递的使用更容易。

于 2013-04-10T13:43:58.437 回答