1

I'm trying to do a mysql dump from apache commons-exec and i get the following error

Exception in thread "main" java.io.IOException: Cannot run program "cmd.exe \c" (in directory "."): CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(ProcessBuilder.java:470) at java.lang.Runtime.exec(Runtime.java:593) at org.apache.commons.exec.launcher.Java13CommandLauncher.exec(Java13CommandLauncher.java:61) at org.apache.commons.exec.DefaultExecutor.launch(DefaultExecutor.java:279) at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:336) at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166) at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:153) at com.etq.e2mc.platform.windows.WindowsProcess.execCommons(WindowsProcess.java:87) at com.etq.e2mc.platform.windows.WindowsProcess.main(WindowsProcess.java:79) 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.(ProcessImpl.java:177) at java.lang.ProcessImpl.start(ProcessImpl.java:28) at java.lang.ProcessBuilder.start(ProcessBuilder.java:452) ... 8 more

This is the code i'm using, its pretty simple and straight forward but i can't figure out why it's not invoking the cmd (NOTE: tried with invoking mysql dump directly without the cmd and i get the same type of error), would appreciate any help

    public static void main(String[] args) throws Exception {
        execCommons();
    }

    public static void execCommons() throws ExecuteException, IOException {
        CommandLine cmd = new CommandLine("cmd.exe /c");
        cmd.addArguments("mysqldump");
        cmd.addArguments(new String[] { "-u", "root", " -P", "3316", " -h", "localhost", " -A", ">"});
        cmd.addArguments("\"G:\\test.sql \"" , false);
        new DefaultExecutor().execute(cmd);

    }
user1
  • 254
  • 1
  • 15
  • 1
    can you launch `cmd` from `Run` ? Which version of Windows? is `cmd` location in `PATH` variable? – Sabir Khan May 09 '16 at 12:46
  • @SabirKhan everything runs fine from Run anywhere, mysqld dump is added to the PATH, and i can open cmd, mysqldump just fine from Run! – user1 May 09 '16 at 12:50

2 Answers2

2

for some reason commons-exec doesn't seem to like having the command phrased as it was in the question (initializing CommandLine with "cmd.exe /c"), everything seems to work just fine after rephrasing it to the following

    public static void main(String[] args) throws Exception {
        execCommons();
    }

    public static void execCommons() throws ExecuteException, IOException {
        CommandLine cmd = new CommandLine("cmd.exe ");
        cmd.addArgument("/c");
        String command = "mysqldump " + "-u" + "root" + " -P" + "3316" + " -h" + "localhost" + " -A >" + "\"G:\\test.sql \"";
        cmd.addArgument(command,false);
        new DefaultExecutor().execute(cmd);    
    }

i have no idea why this works the way it does as there are no clarification in the docs but am leaving this here in case it helps someone. but if anyone have any ideas please do tell

user1
  • 254
  • 1
  • 15
0

execute external program from (plain) java, in windows:

Process p = Runtime.getRuntime().exec("cmd /c start /wait \"title\" \""+exe_path_and_other_parameters+"\"");
p.waitFor();
Exceptyon
  • 1,584
  • 16
  • 23
  • i tried this approach and it's waiting forever and never even executing the dump which is why i tried using apache commoons-exec – user1 May 09 '16 at 12:48
  • you can also remove the .waitFor() if you don't need to read the output in your program. or write a .bat (with echo-es) and call that one. anyway, this should work... are you sure that manual launching mysqldump works fine? – Exceptyon May 09 '16 at 12:52
  • am sure it works fine, and it doesnt matter if i add waitfor or not the mysql dump just never executes using the approach you mentioned – user1 May 09 '16 at 12:55