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.

3
  • 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? Commented Aug 25, 2018 at 19:18
  • Runtime.exec is not a shell; dupe stackoverflow.com/questions/31776546/… Commented Aug 26, 2018 at 1: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. Commented Aug 26, 2018 at 16:33

1 Answer 1

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(); 
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.