I am using Java and Mysql for a Program, I am Using a Script File in order to restore a Databsae.
Under Java I am Executing a command:under bin: mysql -u root -proot test< c:\test.mysql
It is not running while If I run it under cmd line it will execute properly and restore the database.
Is anybody there who knows.. why it happens..
Whats the problem, why its not running if i run it under Java environment.
EXACT SYNTAX:
I m Using Process P= runtime.getRunTime().exec(FilePath)
where FilePath Variable is having value: mysql -u root -proot test< c:\test.mysql
I am Using Windiws environment. while if I run the FilePath in CmdLine, it will give the perfect reesult.
Highly thankful or help.
I had the same problem!
Actually the only thing that I could make work (on Windows, havent tested other platforms) is 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();
}}
I think we need some more information. As long as the paths are set up the same, if it will run from the command line, it should run the same from Runtime.exec(). What errors do you see?
Try setting the commend up in a script so you can echo the paths and the command output to a file to look at later. In 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're using Windows, so I don't know how to set of the command file exactly this way; what's important is to make sure you're sending the error output of the mysql commend to the file.