Java 从文件加载 sqlite3 数据库的系统命令失败

Java system command to load sqlite3 db from file fails

我正在尝试使用 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

我已经搜索了很多 运行ning 系统命令的例子,但找不到任何可以解释这个错误的东西,至少对我来说是这样!

我在那里尝试了其他命令,如 ps 等等,它们似乎 运行 没问题。

有什么建议吗?

如果这很重要,我会 运行在 MAC 上解决这个问题。

您好,您需要使用 ProcessBuilder 运行 重定向输入。应该围绕这些线。

ProcessBuilder builder = new ProcessBuilder("sqlite3", database);
builder.redirectInput(file);
Process p = builder.start();