我正在编写一个程序,在第一次执行时将自己复制到特定文件夹,在 linux 或 windows 中工作。
在 linux 中它工作得很好但是当我尝试在 windows 上做同样的事情时我得到以下错误:
java.nio.file.FileSystemException: The process cannot access the file because it is being used by another process (in sun.nio.fs.WindowsException)
那么,另一个进程是程序本身,我应该用什么来跳过这个错误呢?
我的代码行是:
public void installProgram (){
System.out.println("Doing Install...");
File fileToBeInstalled = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
try {
Files.move(fileToBeInstalled.toPath(), installPathFile.toPath(), REPLACE_EXISTING);
} catch (IOException ex) {
MainClass.getMainClass(InstallerLinux.class.getName()).log(Level.SEVERE, null, ex);
}
}
谢谢!
最佳答案
好的, 我没有找到完美的解决方案,但有些东西......
try {
//Files.move(fileToBeInstalled.toPath(), installPathFile.toPath(), REPLACE_EXISTING);
Files.copy(fileToBeInstalled.toPath(), installPathFile.toPath(), REPLACE_EXISTING);
fileToBeInstalled.delete();
} catch (IOException ex) {
MainClass.getMainClass(InstallerLinux.class.getName()).log(Level.SEVERE, null, ex);
}
只有在 linux 执行时才能正确复制文件并正确删除原始文件。
我认为我需要使用类加载器调用类..
关于java.nio.文件.FileSystemException : The process cannot access the file because it is being used by another process,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33924479/