I'm trying to execute a script .sql generated with MySQL from a java application, I have read and researched different ways to do it and I have found several options (none of which had worked for me) and I found the following block of code :
private void executeScript (String host, String dbuser,
String dbpassword, String scriptpath) {
try {
String[] cmd = new String[]{"mysql",
" --host="+host,
" --user=" + dbuser,
" --password=" + dbpassword,
" -e ",
"\"source " + scriptpath + "\""
};
//Aqui intente poner el commando directamente
//String cmd = "mysql --host=192.168.0.13 --user=root_remote --password=root -e \'source "+scriptpath+"\'";
System.err.println(cmd[0]+cmd[1]+cmd[2]+cmd[3]+cmd[4]+cmd[5]);
Process proc = Runtime.getRuntime().exec(cmd);
//Leemos los errores de la consola
InputStream inputstream = proc.getErrorStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
String line;
while ((line = bufferedreader.readLine()) != null) {
System.out.println(line);
}
try {
if (proc.waitFor() != 0) {
System.err.println("exit value = " +
proc.exitValue());
}
}
catch (InterruptedException e) {
System.err.println(e);
}
} catch (Exception e) {
e.printStackTrace();
}
}
The block above worked correctly in Windows, but at the time of testing in an installation of Ubuntu I get an error saying that the database is unknown, but nevertheless when executing the command directly in the terminal it works correctly, as you can see in the following image:
I tried to run the command directly in Runtime.exec (), as you can see in the example and the same thing happens, I have also tried to delete the slash "\" before source and at the end and it does not work either, I repeat in windows it works correctly, the problem is in linux, I hope you can help me.