Rebuild MYSQL from CMD line using Java environment
I am using Java and Mysql for the program, I am using Script file to restore Databsae.
In Java I am running the command: under bin: mysql -u root -proot test <C: \ test.mysql
He does not work. If I run it on cmd line it will execute correctly and restore the database.
Does anyone know why this is happening .. what is the problem, why it doesn't work if I run it in a Java environment.
EXTRACT SYNTAX: I m Using process P = runtime.getRunTime (). Exec (FilePath)
where the FilePath variable is set to: mysql -u root -proot test <c: \ test.mysql
I am using Windiws environment. and if I run FilePath in CmdLine it gives excellent results.
Very grateful or help.
a source to share
I had the same problem!
Actually, the only thing I could do (on Windows, did not test other platforms) was using batch files:
here is the code:
public class MysqlDatabase {
private int BUFFER = 10485760;
private String host, port, user, password, db;
public MysqlDatabase(String host, String port, String user, String password, String db) {
this.host = host;
this.port = port;
this.user = user;
this.password = password;
this.db = db;
}
public boolean restoreDatabase(String filepath) throws Exception {
String comando = "mysql " + db + " --host=" + host + " --port=" + port
+ " --user=" + user + " --password=" + password
+ " < " + filepath;
File f = new File("restore.bat");
FileOutputStream fos = new FileOutputStream(f);
fos.write(comando.getBytes());
fos.close();
Process run = Runtime.getRuntime().exec("cmd /C start restore.bat ");
return true;
}
public String getFull() throws Exception {
Process run = Runtime.getRuntime().exec(
"mysqldump --host=" + host + " --port=" + port
+ " --user=" + user + " --password=" + password
+ " --opt "
+ "" + db);
InputStream in = run.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuilder temp = new StringBuilder();
int count;
char[] cbuf = new char[BUFFER];
while ((count = br.read(cbuf, 0, BUFFER)) != -1) {
temp.append(cbuf, 0, count);
}
br.close();
in.close();
return temp.toString();
}}
a source to share
I think we need more information. As long as the paths are set up the same, if run from the command line it should work from Runtime.exec (). What errors do you see?
Try setting the score in your script so you can iterate over the paths and command output to a file to watch later. On UNIX that would look like
LOGFILE=my.log
echo $PATH > $LOGFILE
env >> $LOGFILE
mysql -u root -proot test< c:\test.mysql >> $LOGFILE 2>&1
It looks like you are using Windows, so I donβt know how to install the batch file this way; which is important to make sure you send the error output in mysql to a file.
a source to share