2

I'm trying to use java to run a system command to load a sqlite3 database from a sql file. There is no error in the sql file, it loads ok using the usual method from the command line:

sqlite3 dbname < file.sql

My method:

    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 + ")");
    }

  }

The command being run translates to:

/usr/bin/sqlite3 /tmp/infinite_state_machine_root/1535223603610/control/database/ism.db < /tmp/ISMCoreActionPack_sqlite3.sql

And the error I see on stdout is:

Error: near "<": syntax error

I've searched for a lot of examples of running system commands and can't find anything that explains this error, at least to me!

I have tried other commands in there like ps and so on and they seem to run ok.

Any advice?

I'm running this on a MAC if that is significant.

Kaliklipper
  • 355
  • 5
  • 19
  • Is there a reason you're trying to do all this through a command line instead of using Java's JDBC API to communicate with the database? – Zephyr Aug 25 '18 at 19:18
  • `Runtime.exec` is not a shell; dupe https://stackoverflow.com/questions/31776546/why-does-Runtime-exec-work-for-some-but-not-all-commands – dave_thompson_085 Aug 26 '18 at 01:46
  • Hi @Zephyr Yes, the reason I am doing it through the command line is because I can't find a way of doing it via jdbc. The command to import it is .read /path/file from memory. I couldn't find a way of getting jdbc to run . commands. It seems to be purely dedicated to SQL. – Kaliklipper Aug 26 '18 at 16:33

1 Answers1

1

Hi you would need to use ProcessBuilder to run redirect on input. Should be something around these lines.

ProcessBuilder builder = new ProcessBuilder("sqlite3", database);
builder.redirectInput(file);
Process p = builder.start(); 
Alexander Petrov
  • 9,204
  • 31
  • 70
  • Many thanks Alex, that nailed it. :) I'll read up on process builder and see what I can do in the way. of error checking and so on. – Kaliklipper Aug 25 '18 at 20:41