0

** The following command works fine when run directly on terminal

mysqldump -uabc -pabc1234 --compact --no-create-info -w \"fieldname != 'A'\" dbname tablename -hhostaddress --result-file=/tmp/myfile.txt

** But when it is executed using Runtime() method then it does not produce the output in the destination file.

String s="mysqldump -uabc -pabc1234 --compact --no-create-info -w \"fieldname != 'A'\" dbname tablename -hhostaddress --result-file=/tmp/myfile.txt";
Runtime.getRuntime().exec(s);

(** say abc is the username and abc1234 the password)

The same problem occurs if redirection to the destination file ( > ) is usedinstead of --result-file option. What shall i do to execute it from within a java program ?

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
richa.a91
  • 11
  • 1
  • 8
  • Just a guess: maybe you should try to put the file-full path in quotes: `\"/tmp/myfile.txt\"`. You can also redirect the output into a file like described [*here*](http://go2linux.garron.me/mysql-backup-mysqldump) – Nir Alfasi Dec 31 '12 at 04:36
  • You need to split the command into parts, instead of sending it all as one long string. – Perception Dec 31 '12 at 04:38

2 Answers2

0

Use ProcessBuilder api which handles whitespaces better. Output will be in result file.

 new ProcessBuilder( "mysqldump" , "-uabc", "-pabc1234", ... ).start();

'>' is Shell indirection- It works if your program is shell.

new ProcessBuilder( "bash", -"c "mysqldump" , "-uabc", "-pabc1234", ..., "> fileresult"  ).start();
Jayan
  • 18,003
  • 15
  • 89
  • 143
0

for unachiveing file I used

final Runtime r = Runtime.getRuntime();
            final Process p = r.exec("/bin/tar -xvf " + zipFile, null, fileDir);

where zipFile - target filename, fileDir - current dir path (where need to put files)

Also I used "/bin/tar" because direct "tar" was hidden from Runtime.exec environment. Try to use direct path too (just for test).

for logs just add next at the end

        final int returnCode = p.waitFor();

        if (logger.isDebugEnabled()) {
            final BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = is.readLine()) != null) {
                logger.debug(line);
            }
            final BufferedReader is2 = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            while ((line = is2.readLine()) != null) {
                logger.debug(line);
            }
        }
iMysak
  • 2,170
  • 1
  • 22
  • 35