chettyharish
chettyharish

Reputation: 1734

Error in executing mysql command from java

I was trying to execute the mysql command using java but this always keeps giving error.

    String dbName = "dth";
    String dbUser = "root";
    String dbPass = "root";

    String executeCmd = "";
    executeCmd = "mysqldump -u " + dbUser + " -p" + dbPass + " " + dbName + " -r    C:\\backup.sql";

    Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
    int processComplete = runtimeProcess.waitFor();
    if (processComplete == 0) {
        System.out.println("Backup taken successfully");
    } else {
        System.out.println("Could not take mysql backup");
    }

} catch (Exception e) {
    System.out.println(e.getMessage());
}

I tried the above code but I couldn't get it done, keeps giving me

CreateProcess error=2, The system cannot find the file specified error.

I have tried it by creating a .sql file on the location and still I get the same error.

Upvotes: 1

Views: 255

Answers (1)

Bushbert
Bushbert

Reputation: 158

Add the directory in which mysqldump resides into your path environment variable or use the full pathname I.e c:\directory\mysqldump

Upvotes: 1

Related Questions