2

When this code executes, the file specified in the redirect does not get created. Why not?

public class MyDdlSql {
    public static void main(String[] args) {
        try {
            Runtime.getRuntime().exec("mysqldump -uroot -psuri biztime >D:data.sql");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
BaseZen
  • 8,650
  • 3
  • 35
  • 47
  • It looks like you're using windows. You're using relative path. So you have to look in your current directory (under drive D) or use an absolute path like D:\\data.sql (you have to quote the backslash, or you can use forward slash). – dev.null Aug 29 '15 at 05:34
  • Also you need to run the program with Administrative permission – The Coder Aug 29 '15 at 06:03
  • that is also not working...... file not created – Surendra kumar Kadiri Aug 30 '15 at 13:53
  • package scheduler; import java.io.IOException; public class MyDdlSql { public static void main(String[] args) { try { Process p=Runtime.getRuntime().exec("mysqldump -uroot -psuri biztime >D:\\data22.sql"); int i=p.waitFor(); if(i==0){ System.out.println("success"); } else{ System.out.println("fail"); } } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } this give the fail and file not created in specified location.......... – Surendra kumar Kadiri Aug 30 '15 at 13:59

1 Answers1

1

Look here:

Runtime's exec() method is not redirecting the output

Redirection is a feature of the shell. So you cannot use it. Instead you should use ProcessBuilder.

ProcessBuilder builder = new ProcessBuilder("mysqldump", "-uroot", "-psuri", "biztime");
builder.redirectOutput(new File("D:\\data.sql"));
builder.redirectError(new File("error.log"));
Process p = builder.start();
Community
  • 1
  • 1
  • package scheduler; import java.io.IOException; public class MyDdlSql { public static void main(String[] args) { try { Process p=Runtime.getRuntime().exec("mysqldump -uroot -psuri biztime >D:\\data22.sql"); int i=p.waitFor(); if(i==0){ System.out.println("success"); } else{ System.out.println("fail"); } } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } this give the fail and file not created in specified location.......... – Surendra kumar Kadiri Aug 30 '15 at 14:05