-2

I have following command that I use from commandline to import a mysql backup -

mysql -hlocalhost -uroot -pmysql dpfinal < /home/kshitiz/Desktop/dbdump.sql

When I invoke this command from command line it runs fine. But when I use following Java code it doesn't work -

String command = "mysql -hlocalhost -uroot -pmysql dpfinal < /home/kshitiz/Desktop/dbdump.sql";
InputStream is = Runtime.getRuntime().exec(command).getInputStream();
InputStreamReader ir = new InputStreamReader(is);
BufferedReader br = new BufferedReader(ir);
String s = br.readLine();
while(s!= null) {
    System.out.println(s);
    s = br.readLine();
}

I get following output -

mysql  Ver 14.14 Distrib 5.5.28, for debian-linux-gnu (i686) using readline 6.2
Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Usage: mysql [OPTIONS] [database]
  -?, --help          Display this help and exit.
  -I, --help          Synonym for -?
  --auto-rehash       Enable automatic rehashing. One doesn't need to use
                      'rehash' to get table and field completion, but startup
                      and reconnecting may take a longer time. Disable with
                      --disable-auto-rehash.
                      (Defaults to on; use --skip-auto-rehash to disable.)
  -A, --no-auto-rehash 
                      No automatic rehashing. One has to use 'rehash' to get
                      table and field completion. This gives a quicker start of
                      mysql and disables rehashing on reconnect.
  --auto-vertical-output 
                      Automatically switch to vertical output mode if the
                      result is wider than the terminal width.
  -B, --batch         Don't use history file. Disable interactive behavior.
                      (Enables --silent.)
  --character-sets-dir=name 
                      Directory for character set files.
  --column-type-info  Display column type information.
Kshitiz Sharma
  • 17,947
  • 26
  • 98
  • 169

1 Answers1

2

The mysql command is not the one which interprets stdin redirection (<). For that to work you must execute a shell program and pass to it as argument the command you wish to execute (mysql).

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • 1
    On top of that, command line options need to be separate entries in the commandline array passed to [Runtime.exec()](http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String[])) – Mark Rotteveel Sep 18 '13 at 09:15
  • 1
    `Runtime.exec(new String[] { "sh", "-c", "mysql -hlocalhost -uroot -pmysql dpfinal < /home/kshitiz/Desktop/dbdump.sql" });` – nos Sep 18 '13 at 09:15
  • 1
    You can use the -D and -e options to run a SQL script: `mysql -hlocalhost -uroot -pmysql -D dpfinal -e "source dbdump.sql"` – Taj Morton Sep 18 '13 at 09:18
  • @Nos That does not work either. And there is no output. – Kshitiz Sharma Sep 18 '13 at 10:10
  • @TajMorton That too works from command line but not from Java. – Kshitiz Sharma Sep 18 '13 at 10:12
  • @KshitizSharma Did you pass them as individual arguments? `Runtime.exec(new String[] {"mysql", "-hlocalhost", "-uroot", "-pmysql", "-D", "dpfinal", "-e", "source dbdump.sql"})` – Taj Morton Sep 18 '13 at 16:23