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); } else { // 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