4

我尝试运行以下代码来创建数据库的备份,但它显示了一些运行时错误。

但是,我尝试在 mysql shell中运行System.out.println() 输出部分(我在给定的代码中对此进行了注释)并且它有效。

它显示io文件问题。请有人帮助我。

package files;

 public class tableBackup_1 {

public boolean tbBackup(String dbName,String dbUserName, String dbPassword, String path) {

    String executeCmd = "mysqldump -u " + dbUserName + " -p" + dbPassword + " --add-drop-database -B " + dbName + " -r " + path;
    Process runtimeProcess;
        try
        {
            System.out.println(executeCmd);//this out put works in mysql shell
            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

                if (processComplete == 0)
                {
                    System.out.println("Backup created successfully");
                    return true;
                }
                else
                {
                    System.out.println("Could not create the backup");
                }
        } catch (Exception ex)
        {
            ex.printStackTrace();
        }
return false;
}

public static void main(String[] args){

        tableBackup_1 bb = new tableBackup_1();
        bb.tbBackup("test","harin","1234","C:/Users/Master/Downloads/123.sql");

}
}
    mysqldump -u harin -p1234 --add-drop-database -B test -r C:/Users/Master/Downloads/123.sql
java.io.IOException: Cannot run program "mysqldump": CreateProcess error=2, The system cannot find the file specified
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
        at java.lang.Runtime.exec(Runtime.java:593)
        at java.lang.Runtime.exec(Runtime.java:431)
        at java.lang.Runtime.exec(Runtime.java:328)
        at files.tableBackup_1.tbBackup(tableBackup_1.java:12)
        at files.tableBackup_1.main(tableBackup_1.java:34)
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.<init>(ProcessImpl.java:81)
        at java.lang.ProcessImpl.start(ProcessImpl.java:30)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
        ... 5 more
4

3 回答 3

6

请检查您的全局 PATH 环境变量中是否包含 <Path to MySQL>\bin (执行echo %PATH%并查看)。实际上,您应该能够在普通 DOS 提示符下键入 System.out.println() 内容并且应该能够运行它。

即使这样,如果它不起作用,请尝试更改代码以执行如下所示

runtimeProcess = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", executeCmd });

理想情况下,这应该可以解决问题。

更新:

如果 PATH 环境变量中没有它,请将代码更改为以下

String executeCmd = "<Path to MySQL>/bin/mysqldump -u " + dbUserName + " -p" + dbPassword + " --add-drop-database -B " + dbName + " -r " + path;
于 2012-11-14T09:47:58.887 回答
0

runtimeProcess = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", executeCmd });

我试过这个,但没有用,所以我把它换成了

this runtimeProcess = Runtime.getRuntime().exec(executeCmd);

它奏效了

于 2013-09-01T18:55:33.720 回答
0

我通过给出 mysqldump.exe 的完整路径解决了这个问题

您可以通过以下方式获取 SO 环境变量

Map<String, String> env = System.getenv();
final String LOCATION = env.get("MYSQLDUMP");

我像这样设置系统变量:

  • 变量的名称 :MYSQLDUMP
  • 价值 :D:\xampp\mysql\bin\mysqldump.exe

现在只需执行下面的代码,

String executeCmd = LOCATION+" -u " + DBUSER + " --add-drop-database -B " + DBNAME + " -r " + PATH + ""+FILENAME
Process runtimeProcess = = Runtime.getRuntime().exec(executeCmd);

注意:如果您没有为 mysql 服务器配置密码,只需-p PASSWORD从代码中删除该属性。并且不要忘记在创建新的系统变量后重新启动计算机。

于 2017-03-14T05:28:32.677 回答