Possible Duplicate:
Redirection with Runtime.getRuntime().exec() doesn’t work
I'm trying to run a SQL script with Java on OS X to initiate my database for testing. I know it's not the preferred way to do it, but it's a temporary solution.
I've tried to read about the exec()
function, and how to pass parameters, but I simply can't make it work.
My code looks like this:
try {
Process p = Runtime.getRuntime().exec("/usr/local/mysql/bin/mysql -uroot dev_test <div/test_db.sql");
p.waitFor();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
I've also tried to pass the arguments in a string array, but with no luck.
I've managed to make it run on a windows machine by using this format, but I can't change it so that it works in osx:
Process p = Runtime.getRuntime().exec(new String[] { "cmd", "/C", "mysql -u root dev_test < div\\test_db.sql" });