Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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();
–
–
You can test the below mentioned code for testing your mysqldump command output. There can be two main reasons to why the file is not creating, per my assumptions:-
If using windows then the UAC permissions for the destined location can be the issue.
You might be facing a syntax issue in the final mysqldump command generated to be executed by the java runtime.
//Process exec = Runtime.getRuntime().exec("C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump "+fisier.getName()+" > C:\\"+fisier.getName()+".sql;");
Process exec = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump "+fisier.getName()+" > C:\\"+fisier.getName()+".sql;"});
//Wait for the command to complete, and check if the exit value was 0 (success)
if(exec.waitFor()==0)
//normally terminated, a way to read the output
InputStream inputStream = exec.getInputStream();
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
String str = new String(buffer);
System.out.println(str);
// abnormally terminated, there was some problem
//a way to read the error during the execution of the command
InputStream errorStream = exec.getErrorStream();
byte[] buffer = new byte[errorStream.available()];
errorStream.read(buffer);
String str = new String(buffer);
System.out.println(str);
The redirection operator doesn't works when using
Process exec = Runtime.getRuntime().exec("C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump "+fisier.getName()+" > C:\\"+fisier.getName()+".sql;");
It is becasue it does not invokes the command shell, so we cannot get the functionality of the redirection operator, in order to fix this we can execute a command prompt (cmd) followed by the mysqldump command, and it will work.
Process exec = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump "+fisier.getName()+" > C:\\"+fisier.getName()+".sql;"});
Here /c in the cmd.exe specify that execute the passed command and terminate the process.
The actual command when executed by java runtime will become
cmd.exe /c yourmysqldumpCommand
–
–
–
–
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.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.