我正在尝试使用 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
我在标准输出上看到的错误是:
Error: near "<": syntax error
我搜索了很多运行系统命令的示例,但找不到任何可以解释此错误的内容,至少对我来说是这样!
我在那里尝试了其他命令,如 ps 等,它们似乎运行正常。
有什么建议吗?
如果这很重要的话,我会在 MAC 上运行它。
您好,您需要使用 ProcessBuilder 对输入运行重定向。应该是围绕这些线的东西。
ProcessBuilder builder = new ProcessBuilder("sqlite3", database);
builder.redirectInput(file);
Process p = builder.start();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)