Executing mysqldump using Java program in MacOS

242 Views Asked by At

I'm using MacOS Mojave and mysqldump is set to path in ~/.bash_profile

When using MacOS Terminal mysqldump is working.

Even with /bin/bash -c mysqldump it's working.

But when I try it with my Java Application as following give error.

Error/Output:

command: [/bin/bash, -c, mysqldump -hlocalhost-u root -p123 some_database -r "/Users/Username/Workspace/backup.sql"] /bin/bash: mysqldump: command not found false

Program code:

String cmd = "mysqldump -h" + dbHost + "-u " + dbUser + " -p" + dbPass + " " + dbName + " -r \"" + file.getAbsolutePath() + "\"";
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", cmd);
pb.redirectErrorStream(true);
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.directory(new File(System.getProperty("user.home")));
System.out.println("command: " + pb.command());  
Process process = pb.start();
int processComplete = process.waitFor();
if (processComplete == 0) {
    return true;
} else {
    return false;
}

I'm using AMPPS MySQL Installation.

[Update]

I think this issue is related to Netbeans. I'm testing from Netbeans. Seems Netbeans not load ~/.bash_profile

1

There are 1 best solutions below

2
Fawzi On

Try using this path to execute mysqldump

/Applications/AMPPS/mysql/bin/mysqldump

your command should be something like this

String cmd = "/Applications/AMPPS/mysql/bin/mysqldump  -h" + dbHost + "-u " + dbUser + " -p" + dbPass + " " + dbName + " -r \"" + file.getAbsolutePath() + "\"";