1
String[] executeCmd = new String[] { "mysql", "-u",DB_USER,"-p"+DB_PASSWORD,DB_NAME, " < ", "\""+FileName+"\"" };

Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
System.out.println("processComplete: " + processComplete);

This is the code I have worked with. The program hangs when the "waitFor()" method is called.

How to solve this?

John Woo
  • 258,903
  • 69
  • 498
  • 492
Gapchoos
  • 1,422
  • 5
  • 20
  • 40

3 Answers3

5

you need to add spaces after the parameters, don't use array

String executeCmd = "mysqldump ", " -u ",DB_USER," -p " + DB_PASSWORD, + " " + DB_NAME, " < ", "\""+FileName+"\"" ;

and the cleaner way is to use String.Format()

String executeCmd = String.Format("mysqldump -u %s -p %s %s < \"%s\"", DB_USER, DB_PASSWORD, DB_NAME, FileName)
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • Are you sure? http://stackoverflow.com/questions/3695230/how-to-use-java-string-format. `String.format("%s does support it!", "java")` – Elbek Sep 28 '12 at 04:23
  • @elber i already edited the answer. the original `string.format` I showed was in `C#` – John Woo Sep 28 '12 at 04:24
  • You said it does not support, not like for C#, that is why i posted. – Elbek Sep 28 '12 at 04:25
  • 2
    @elber ok ok, my mistake. i tried it on netbeans but it failed that's why i said it's not working but i realize the syntax was incorrect. java uses `%s or etc` while `c#` uses `{0}, {1}, etc..` – John Woo Sep 28 '12 at 04:28
  • 1
    A space after `-u` is optional. If you have a space after `-p` it means `mysqldump` will prompt for a password. If the password is specified on the command line (as is the case here) you must not use a space after the `-p` parameter. – Arjan Sep 28 '12 at 04:48
  • @JohnWoo Sorry to say this doesnt solve the issue. still the program gets hang – Gapchoos Sep 28 '12 at 06:30
0
    String[] command = new String[]{"mysql", Constants.DB_NAME, "-u" + Constants.DB_USER, "-p" + Constants.DB_PASSWORD, "-e", " source "+FileName };

    try {
            Process runtimeProcess = Runtime.getRuntime().exec(command);
            int processComplete = runtimeProcess.waitFor();
            if (processComplete == 0)
            {
                System.out.println("DatabaseManager.restore: Restore Successfull");

            }
            else 
            {
                System.out.println("DatabaseManager.restore: Restore Failure!");
            }

        return true;

    }

    catch (final Exception ex) 
    {
        Application.showErrorInConsole(ex);
        NmsLogger.writeErrorLog("Database Restore failed ", ex.toString());
        NmsLogger.writeDebugLog(ex);
        return false;

    }

The above code worked fine for me :)

Gapchoos
  • 1,422
  • 5
  • 20
  • 40
0

In case you don't set a password for XAMPP, don't use -p

public class Testbackupmysql {
public boolean tbBackup(String dbName,String dbUserName, String path) {

    String executeCmd = "C:\\xampp\\mysql\\bin\\mysqldump -u " + dbUserName + " --add-drop-database -B " + dbName + " -r " + path;
    Process runtimeProcess;
        try
        {
            System.out.println(executeCmd);//this out put works in mysql shell
            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

                if (processComplete == 0)
                {
                    System.out.println("Backup created successfully");
                    return true;
                }
                else
                {
                    System.out.println("Could not create the backup");
                }
        } catch (Exception ex)
        {
            ex.printStackTrace();
        }
return false;
}

public static void main(String[] args){

    Testbackupmysql bb = new Testbackupmysql();
        bb.tbBackup("your_database_name","your_db_username","D:\\123.sql");

}

}

msl
  • 109
  • 1
  • 4