gpt4 book ai didi

java.io.IOException :Cannot run program "mysql":CreateProcess error=2, 系统找不到指定的文件

转载 作者:行者123 更新时间:2023-11-28 23:36:06 24 4
gpt4 key购买 nike

我的数据库恢复程序出了点问题,这个错误掩盖了我的快乐:

java.io.IOException: Cannot run program "mysql":CreateProcess error=2, The system cannot find the file specified

要恢复的文件位于 D:/Backup/backup.sql 中,当我从该路径浏览并打开文件时,当我单击恢复按钮时出现错误。请帮我解决这个问题。下面是我使用 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);

}

}


}


);

最佳答案

您应该将“mysql”的路径添加到“路径”变量中或在您的代码中指定完整路径:

尝试

String[] executeCmd = new String[]{"\FULL PATH HERE\mysql",databaseName, "--user=" + userName, "--password=" + password, "-e", "source"+source};

代替

String[] executeCmd = new String[]{"mysql",databaseName, "--user=" + userName, "--password=" + password, "-e", "source"+source};

关于java.io.IOException :Cannot run program "mysql":CreateProcess error=2, 系统找不到指定的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35699614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com