Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysqldump from java application

Tags:

java

mysqldump

Try to backup mysql DB from Java application (IDE Netbeans) using the following command but can't seem to find the file even though I specified a path:

Runtime.getRuntime().exec("C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump "+fisier.getName()+" > C:\\"+fisier.getName()+".sql;");

Also, I don't receive any errors, so I assumed that the backup has been done. How can I find the file? Regarding fisier.getName(): File fisier = jFileChooserSave.getSelectedFile();

like image 864
bluesony Avatar asked Dec 28 '13 22:12

bluesony


Video Answer


1 Answers

For non-windows users:

String dump = "mysqldump -usome_user -psome_pass database_name   > path/to/file.sql";
String[] cmdarray = {"/bin/sh","-c", dump};
Process p = Runtime.getRuntime().exec(cmdarray);
if (p.waitFor() == 0) {
    // Everything went fine
} else {
   // Something went wrong
}

Using the cmd array is important. Otherwise exec cannot parse '>' and file name and you will get an error.

like image 137
StationaryTraveller Avatar answered Sep 30 '22 11:09

StationaryTraveller