[英]java.io.IOException:Cannot run program “mysql”:CreateProcess error=2, The system cannot find the file specified
Something is going wrong with my program for database recovery, this error hides my happiness:我的数据库恢复程序出了点问题,这个错误隐藏了我的快乐:
java.io.IOException: Cannot run program "mysql":CreateProcess error=2, The system cannot find the file specified java.io.IOException:无法运行程序“mysql”:CreateProcess error=2,系统找不到指定的文件
file to be recovered is located in D:/Backup/backup.sql when I browse and open the file from this path then error appears when I click recovery button.要恢复的文件位于 D:/Backup/backup.sql 中,当我从该路径浏览并打开文件时,单击恢复按钮时出现错误。 Please help me solve this problem.请帮我解决这个问题。 below is my code with JFileChooser
for browsing file location.下面是我使用JFileChooser
浏览文件位置的代码。
browseButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
String recPath = "";
JFileChooser fc = null;
if (fc == null) {
fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fc.setAcceptAllFileFilterUsed(false);
}
int returnVal = fc.showDialog(null, "Open");
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
recPath = file.getAbsolutePath();
sourceField.setText(recPath);
}
}
}
);
recoveryButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
try{
String databaseName ="jdbc:mysql://localhost:3306/myDB";
String userName ="abc";
String password ="123";
String source = sourceField.getText();
int processComplete;
String[] executeCmd = new String[]{"mysql",databaseName, "--user=" + userName, "--password=" + password, "-e", "source"+source};
//sava the command in a array
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);// execute the command
processComplete = runtimeProcess.waitFor();// get the result to variable
if(processComplete==1){
JOptionPane.showMessageDialog(null, "Restore Failed");
}
else if(processComplete==0){
JOptionPane.showMessageDialog(null, "Restore Completed");
}
}
catch(Exception ex){
JOptionPane.showMessageDialog(null,ex);
}
}
}
);
You should add path to 'mysql' into 'Path' variables or specify full path in your code:您应该将 'mysql' 的路径添加到 'Path' 变量中或在代码中指定完整路径:
Try尝试
String[] executeCmd = new String[]{"\FULL PATH HERE\mysql",databaseName, "--user=" + userName, "--password=" + password, "-e", "source"+source};
instead of代替
String[] executeCmd = new String[]{"mysql",databaseName, "--user=" + userName, "--password=" + password, "-e", "source"+source};
This answer is correct in 2018/06/07...这个答案在 2018/06/07 是正确的...
String[] executeCmd = new String[]{"\FULL PATH HERE\mysql",databaseName, "--user=" + userName, "--password=" + password, "-e", "source"+source};
A example will be :一个例子是:
String[] restoreCmd = new String[] { "C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin\\mysql ", bd,"--user=" + usuario, "--password=" + password, "-e", "source " + pathToFile }
您可以将“\\FULL PATH To MySQL”添加到环境路径变量中,例如:“C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.