0

I am trying to restore an mysql sql file using java, but I dont have any idea why it's not working. The code is given below.

/*NOTE: Getting path to the Jar file being executed*/
            CodeSource codeSource = DBmanager.class.getProtectionDomain().getCodeSource();
            File jarFile = new File(codeSource.getLocation().toURI().getPath());
            String jarDir = jarFile.getParentFile().getPath();

            /*NOTE: Creating Database Constraints*/
            String dbName = "dth";
            String dbUser = "root";
            String dbPass = "root";

            String restorePath ="\""+ jarDir + "\\backup" + "\\" + s+"\"";


            /*NOTE: Used to create a DOS command*/
            String executeCmd = "";
            executeCmd = "C:\\xampp\\mysql\\bin\\mysql -u" + dbUser + " -p" + dbPass + " --database " + dbName + " < " + restorePath;


            System.out.println(executeCmd);


            Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

            if (processComplete == 0) {
                JOptionPane.showMessageDialog(null, "Successfully restored from SQL : " + s);
            } else {
                JOptionPane.showMessageDialog(null, "Error at restoring");
            }

The code executes but the java swing is stuck on the runtime command. The line outputted by System.out.println is like this.

C:\xampp\mysql\bin\mysql -uroot -proot --database dth < "F:\Final Year Project\Final\build\backup\0_Harish_2013-02-17-20-05-12.sql"

This line works perfectly if I copy and paste it in command line. Dunno why the java swing interface just gets stuck in wait state. (The same query takes like 2 seconds on cmd and on java I have waited for 5minutes).

Edit: I ran the streamgobbler and still no benefit, it still gives Exit Value: 1 which is obviously the problem IllegalThreadStateException how can I solve this?

Edit2:

The gobbling doesnt help as the hang still exists , heres the output of gobbler

OUTPUT>C:\xampp\mysql\bin\mysql  Ver 14.14 Distrib 5.5.27, for Win32 (x86)
OUTPUT>Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
OUTPUT>
OUTPUT>Oracle is a registered trademark of Oracle Corporation and/or its
OUTPUT>affiliates. Other names may be trademarks of their respective
OUTPUT>owners.
OUTPUT>
OUTPUT>Usage: C:\xampp\mysql\bin\mysql [OPTIONS] [database]
OUTPUT>  -?, --help          Display this help and exit.
OUTPUT>  -I, --help          Synonym for -?
OUTPUT>  --auto-rehash       Enable automatic rehashing. One doesn't need to use
OUTPUT>                      'rehash' to get table and field completion, but startup
OUTPUT>       and reconnecting may take a longer time. Disable with
OUTPUT>         --disable-auto-rehash.
OUTPUT>                      (Defaults to on; use --skip-auto-rehash to disable.)
OUTPUT>  -A, --no-auto-rehash 
OUTPUT>                      No automatic rehashing. One has to use 'rehash' to get
OUTPUT>                      table and field completion. This gives a quicker start of
OUTPUT>                      mysql and disables rehashing on reconnect.
OUTPUT> --auto-vertical-output 
OUTPUT>                      Automatically switch to vertical output mode if the
OUTPUT>                      result is wider than the terminal width.
OUTPUT>  -B, --batch         Don't use history file. Disable interactive behavior.
OUTPUT>         (Enables --silent.)
OUTPUT>  --character-sets-dir=name 
OUTPUT>       Directory for character set files.
OUTPUT>  --column-type-info  Display column type information.
OUTPUT>  -c, --comments      Preserve comments. Send comments to the server. The
OUTPUT>                      default is --skip-comments (discard comments), enable
OUTPUT>         

(rest of usage message omitted)

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
chettyharish
  • 1,704
  • 7
  • 27
  • 41

2 Answers2

0

I was able to solve the problem by chanding the String command to a String array command. That made the use of Gobbler redundant.

chettyharish
  • 1,704
  • 7
  • 27
  • 41
-1

This is most likely due to you not reading from the output streams - they will block when their buffers are full. Please read this.
You may want to use something like Commons Exec.
You also have the issue that you are tyring to use the pipe (<), this is a bash construct and will not work!
You need to create a FileInputStream from your file in java and set that as the InputStream in your code. Take a look at this example.

Community
  • 1
  • 1
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • I ran the streamgobbler and still no benefit, it still gives Exit Value: 1 which is obviously the problem IllegalThreadStateException how can I solve this? – chettyharish Feb 17 '13 at 15:46