1

Ok, my Java application is not able to perform a mysqldump backup using the windows runtime enviroment. I have printout code for caught exceptions and I see no exceptions being thrown, looks fine on the surface when the backup code is executed.

I have also tested the mysqldump command in the command-line console; and it works with no issue there.

Code:

Runtime rt = Runtime.getRuntime();
try {
    Process pr = rt.exec("mysqldump -u test --password=pass lager > newBackup.sql");

} catch (IOException ex) {
     System.out.println("IO error Runtime.  "+ex.getMessage());
}

Does anyone know why it will not dump/backup the database? Is there some permission I need to add or something (running windows 7).

4

1 回答 1

0

也许你可以看看这篇关于beetwen exec和命令行的区别的文章(http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html) .

在我这边,我尝试使用该代码捕获 mysqldump --help 命令的输出,并在 javaworld 的文章中找到 Class StreamGoggle:

    public Main() {
    try {
        FileOutputStream fos = new FileOutputStream("c:/tmp/test.txt");
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec("mysqldump --help");
        // any error message?
        StreamGobbler errorGobbler = new StreamGobbler(
                proc.getErrorStream(), "ERROR");

        StreamGobbler outputGobbler = new StreamGobbler(
                proc.getInputStream(), "OUTPUT", fos);

        errorGobbler.start();
        outputGobbler.start();

        int exitVal = proc.waitFor();
        System.out.println("ExitValue: " + exitVal);
        fos.flush();
        fos.close();
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
于 2015-07-18T12:24:05.230 回答