从Java运行导入数据库DOS命令
我正在尝试从Java程序运行数据库导入命令,如下所示:从Java运行导入数据库DOS命令,java,import,dos,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=as
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)
未进行导入。您的导入命令
String
被视为一个命令。试着把代币拆开。还要检查从进程#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
使参数传递的使用更容易。它找不到“imp”程序。确保它在路径中。我将路径添加到Imp.exe,作为F:/app/Administrator/product/11.1.0/db_3/BIN/Imp.exe,但什么都没有发生,错误没有出现,但导入也没有出现。我没有收到任何错误,但什么都没有发生,我希望导入开始发生:PDid您检查流程输出吗-特别是从?没有输出只有:构建成功(总时间:0秒)
@Stanley您正在从输出流中读取。你检查过错误流了吗?我不知道怎么做#初学者#