简体   繁体   English

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

[英]Run Import database DOS command from Java

I am trying to run a database import command from a Java program like this: 我试图从这样的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);
        }
    }
}

The error Output is: 错误输出为:

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

The file askdbinstall.dmp is present because if I Paster the Same Command in CMD, it is importing the database Dump Quite fine. 存在文件askdbinstall.dmp的原因是,如果我在CMD中粘贴相同的命令,它将导入数据库Dump Quite很好。 What is My Mistake? 我的错误是什么?

Added: From Reimius Suggestion I have also tried this: 补充:从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);
   }
   }
} 

Output 输出量

run:
BUILD SUCCESSFUL (total time: 3 seconds)

No Import is taking place. 没有导入正在进行。

Your import command String is being treated as one single command. 您的导入命令String被视为一个命令。 Try breaking up the tokens. 尝试分解令牌。 Also check what is being output from Process#getErrorStream : 还要检查从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);
}

Aside: ProcessBuilder make the use of parameter passing easier. 另外: ProcessBuilder使参数传递的使用更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM