I am using ProcessBuilder to execute a mysqldump from java code
and this is my code
public static void executeCommant(String... command) throws Exception {
ProcessBuilder processBuilder = null;
processBuilder = new ProcessBuilder(command);
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
int resultCode = process.waitFor();
if (resultCode != 0) {
throw new Exception("" + readCommandOutput(process.getInputStream()));
}
}
private static String readCommandOutput(InputStream inputStream) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + System.getProperty("line.separator"));
}
} finally {
br.close();
}
return sb.toString();
}
public static void main(String[] args) throws Exception {
executeCommant("mysqldump -u root -P 3316 -h localhost > G:\\test.sql");
}
Problem is i get the following exception even though when i run the same command from cmd i dont get any problem, and i just cannot figure out why it cannot find the specified file!! PS: i tried with giving the full path for the mysqldump.exe and got the same result
Exception in thread "main" java.io.IOException: Cannot run program "mysqldump -u root -P 3316 -h localhost > G:\test.sql": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:470)
at com.etq.e2mc.platform.windows.WindowsProcess.executeCommant(WindowsProcess.java:46)
at com.etq.e2mc.platform.windows.WindowsProcess.main(WindowsProcess.java:67)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:177)
at java.lang.ProcessImpl.start(ProcessImpl.java:28)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
... 2 more