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
Process p;
String command="mysqldump -u'scmuser' -p'scm$123' --routines db_name  >  /home/ubuntu/wh_demo_db_reset.sql";
p = Runtime.getRuntime().exec(command);

the dump file is not generated and not thrown any error. i am unable to predict the exact issue please help me. thanks in advance.

The metacharacter ">" is implemented by the shell; no shell is involved in running a program with Runtime.exec() so the last two arguments to mysqldump are garbage. Use the array argument form of Runtime.exec(); pass "/bin/sh" as the first argument, "-c" as the second, and your command line as the third; that way the shell metacharacters will be interpreted by /bin/sh.

You can form the cmd array as:

String[] cmdarray = {"/bin/sh","-c",command}; 
Process process = Runtime.getRuntime().exec(cmdArray);
                Thanks ..abhijeet i am working on that ...does it work on windows machine? if not how to make it work on windows also.
– Karunakar
                Feb 24, 2015 at 11:53
                You will need to use following command format for windows:        String[] command = new String[] {"cmd.exe", "/c", "C:/path/to/mysqldump.exe")+" --quick --lock-tables --user=myuser --password=mypwd mydatabase"};       final Process process = Runtime.getRuntime().exec(command);  But you will not be able to redirect the output of command to the file as what we do in linux using '>' operator.  You can do that using BufferedReader/Writer.
– Abhijeet Dhumal
                Feb 24, 2015 at 11:56
        

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.