在下面的代码中,我试图用变量"dumppath“替换转储路径,该变量具有与命令中的字符串相同的确切字符,但是当我用变量替换它时,它总是给出一个IOException,其中说系统找不到指定的文件。但是,通过硬编码的路径,它工作得很好。
public String dumppath = "C:\\wamp\\bin\\mysql\\mysql5.6.12\\bin\\mysqldump";
public void backupDatabase() {//GEN-FIRST:event_jButton1ActionPerformed
try {
int processComplete; // to verify that either process completed or not
//Process runtimeProcess = Runtime.getRuntime().exec("C:\\wamp\\bin\\mysql\\mysql5.6.12\\bin\\mysqldump -u root mysql -r F:/backup.sql");
Process runtimeProcess = Runtime.getRuntime().exec("'"+dumppath+"' -u root mysql -r F:/backup.sql");
// call the mysqldump in terminal and execute it
processComplete = runtimeProcess.waitFor();//store the state in variable
if (processComplete == 1) {//if values equal 1 process failed
JOptionPane.showMessageDialog(null, "Backup Failed");//display message
} else if (processComplete == 0) {//if values equal 0 process failed
System.out.println("Backup up : "+backuppath);
System.out.println("Dump Path : "+dumppath);
JOptionPane.showMessageDialog(null, "\n Backup created Successfully..\n Check the Backup File in the D: Directory named as backup.sql");
//display message
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);//exeception handling
}
// TODO add your handling code here:
}发布于 2014-07-31 06:44:42
Process runtimeProcess = Runtime.getRuntime().exec( "'"+dumppath+"' -u root mysql -r F:/backup.sql");是一样的
Process runtimeProcess = Runtime.getRuntime().exec(
"'C:\\wamp\\bin\\mysql\\mysql5.6.12\\bin\\mysqldump' -u root mysql -r F:/backup.sql");与硬编码字符串相比,它有两个更多的单引号。
要获得原始字符串,您应该尝试如下所示
String dumppath = "C:\\wamp\\bin\\mysql\\mysql5.6.12\\bin\\mysqldump";
Process runtimeProcess = Runtime.getRuntime().exec( dumppath+" -u root mysql -r F:/backup.sql");或
String command = "C:\\wamp\\bin\\mysql\\mysql5.6.12\\bin\\mysqldump -u root mysql -r F:/backup.sql";
Process runtimeProcess = Runtime.getRuntime().exec(command); 发布于 2014-07-31 05:32:16
Process runtimeProcess = Runtime.getRuntime().exec(
"C:\\wamp\\bin\\mysql\\mysql5.6.12\\bin\\mysqldump -u root mysql -r F:/backup.sql");
Process runtimeProcess = Runtime.getRuntime().exec(
"'"+dumppath+"' -u root mysql -r F:/backup.sql");这些调用中的第一个在路径周围没有单引号。
https://stackoverflow.com/questions/25051436
复制相似问题