我正在尝试使用Java运行系统命令以从sql文件加载sqlite3数据库。 sql文件中没有错误,它可以使用命令行中的常用方法来正常加载:
sqlite3 dbname < file.sql
我的方法:
public void loadSqlFile(String file, boolean tearDown) {
String s = null;
try {
Process p = Runtime.getRuntime().exec(
"/usr/bin/sqlite3 " +
database +
" < " +
file
);
p.waitFor();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
System.exit(0);
} catch (IOException e) {
LOGGER.error("Failed to load sql file (" + file + ")");
} catch (InterruptedException e) {
LOGGER.error("Failed to load sql file (" + file + ")");
}
}
正在运行的命令翻译为:
/usr/bin/sqlite3 /tmp/infinite_state_machine_root/1535223603610/control/database/ism.db < /tmp/ISMCoreActionPack_sqlite3.sql
我在stdout上看到的错误是:
Error: near "<": syntax error
我已经搜索了很多运行系统命令的示例,但至少找不到我能解释该错误的任何信息!
我尝试了其他命令,例如ps等,它们似乎运行正常。
有什么建议吗?
如果有意义,我正在MAC上运行它。
答案 0 :(得分:1)
您好,您将需要使用ProcessBuilder在输入上运行重定向。应该围绕这些界限。
ProcessBuilder builder = new ProcessBuilder("sqlite3", database);
builder.redirectInput(file);
Process p = builder.start();