简体   繁体   中英

Cannot run mysqldump in XAMPP

I am not able to do an mysqldump in xampp, it says "The system can not find the file specified". I'm using the following code:

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

        String[] executeCmd = new String[] {
                "mysqldump.exe",
                "-u",
                dbUserName,
                "-p",
                dbPassword,
                "--add-drop-database",
                "-B",
                dbName,
                "-r",
                path
            };

        Process runtimeProcess;
        try {

            File pasta = new File("C:\\xampp\\mysql\\bin");
            runtimeProcess = Runtime.getRuntime().exec(executeCmd, null, pasta);
            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;
    }

When I run this, I get the following error:

    java.io.IOException: Cannot run program "mysqldump.exe" (in directory "C:\xampp\mysql\bin"): CreateProcess error=2, O sistema não pode encontrar o arquivo especificado
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
        at java.lang.Runtime.exec(Runtime.java:620)
        at xd.test.backupDB(test.java:25)
        at xd.ee.main(ee.java:7)

Setting one is the mysqldump and it is the right type of extension, it still does not find mysqldump. I tried everything and could not solve this, any ideas?

Use a ProcessBuilder instead, try this:

String[] executeCmd = new String[] {
        "C:\\xampp\\mysql\\bin\\mysqldump.exe ",
        "-u ",
        dbUserName,
        "-p ",
        dbPassword,
        "--add-drop-database ",
        "-B ",
        dbName,
        "-r ",
        path
};

final ProcessBuilder pb = new ProcessBuilder(Arrays.asList(executeCmd));
final Process p;
try {
    p = pb.start();
    int processComplete = p.waitFor();

    if (processComplete == 0) {
        System.out.println("Backup created successfully");
        return true;
    } else {
        System.out.println("Could not create the backup");
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        String line = null;
        while ( (line = reader.readLine()) != null) {
            System.out.println(line);
        }
    }
} catch (IOException | InterruptedException e) {
    e.printStackTrace();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM