0

i am trying this command in java

executeCmd = DBConfig.dbLocation + "\\mysqldump -u " + mysqlUser +
    " -p"+DBConfig.dbPass + " " + DBConfig.dbName + 
    " -r -opt>supervisorDbBkup.sql";

Its giving me code 1 instead of 0

            String executeCmd = "";
    executeCmd = DBConfig.dbLocation+"\\mysqldump -u"+mysqlUser+
            " -p"+DBConfig.dbPass+" "+DBConfig.dbName+" -r -opt>supervisorDbBkup.sql";

    Process runtimeProcess =Runtime.getRuntime().exec(executeCmd);
    int processComplete = runtimeProcess.waitFor();
    if(processComplete == 0){
        return "Backup taken successfully";
    } else {
        return "Could not take mysql backup";
    }

this is the command which i am running :

                E:\program file\MySQL\MySQL Server 5.5\bin\mysqldump -uroot -p0502 dashboardsupervisor -r -opt>supervisorDbBkup.sql

any idea

7
  • 1
    Which OS do you use? Show use how you'll execute executeCmd. Commented Feb 27, 2013 at 7:53
  • win7 , i have pasted the code above Commented Feb 27, 2013 at 7:56
  • Does DBConfig.dbLocation point to MYSQL_HOME/bin ? Commented Feb 27, 2013 at 7:57
  • yes , its : public static String dbLocation ="E:\\program file\\MySQL\\MySQL Server 5.5\\bin"; Commented Feb 27, 2013 at 7:59
  • this is the command which i am running : E:\program file\MySQL\MySQL Server 5.5\bin\mysqldump -uroot -p0502 dashboardsupervisor -r -opt>supervisorDbBkup.sql Commented Feb 27, 2013 at 8:00

3 Answers 3

2

What you need to do is to print out the contents of executeCmd after you've built it, to see what it contains, with something like:

Console.Out.WriteLine (executeCmd);

If that command doesn't work at the command line exactly as you've constructed it, it's unlikely to work within your program. Copy and paste the command exactly as it's output and attempt to execute it from a cmd shell.

I suspect your problem lies with the path to the executable or one of the options.

If that's not the case, you need to watch out for redirection. Redirection is a shell feature which may not be available to the Runtime.exec() call. What you will find is that the redirection bit is being passed as a parameter to the mysqldump execuatble which won't understand it at all.

The usual way to do this is to call the shell itself with arguments telling it how to run the command and redirect properly, something like:

String[] cmd = {"/bin/sh", "-c", "/path/to/mysqldump blah blah >/tmp/tempout"};
Process p = Runtime.getRuntime().exec(cmd);

(or equivalent cmd /c for Windows).

That ensures that redirection is handled correctly (by the shell itself) and the mysqldump executable only gets the arguments it understands.

Sign up to request clarification or add additional context in comments.

3 Comments

if i run the same command in shell , it works fine .. it creates a new file in the bin folder , in java i am expecting it to save the new file in the war folder..
thats what i am getting : java.io.IOException: Cannot run program "E:/program file/MySQL/MySQL Server 5.5/bin/sh": CreateProcess error=2, The system cannot find the file specified
its working with this code : String executeCmd = “”; executeCmd = “mysqldump -u “+dbUser+” -p”+dbPass+” “+dbName+” -r backup.sql”;
1

Try the following

executeCmd = "<Absolute Path to MYSQL>\bin" + "\\mysqldump -u " + mysqlUser +
    " -p"+DBConfig.dbPass + " " + DBConfig.dbName + 
    " -r --opt > supervisorDbBkup.sql";

UPDATE:

-opt is not an option for mysqldump following is

--opt

2 Comments

will there be a single \ before bin ??
Print the output of runtimeProcess using runtimeProcess.getInputStream(); to better understand the exception
0

try with this command "C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqldump -u username -pPASS_WORD database_name -r backup.sql"

Mind the space between -u and username and -p and password(there should be no space between -p and password). It worked for me.

Comments

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.