java.nio.file.FileSystemException:进程无法访问该文件,因为它正由另一个进程使用

sel*_*lan 13 java windows

我正在编写一个程序,在第一次执行时将它们复制到特定文件夹,在linux或windows中工作.
在Linux中它完美地工作但是当我尝试在Windows上执行相同操作时,我收到以下错误:

java.nio.file.FileSystemException:进程无法访问该文件,因为它正由另一个进程使用(在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);

    }
} 
Run Code Online (Sandbox Code Playgroud)

谢谢!

sel*_*lan 8

好吧,我没有找到完美的解决方案,但有些东西......

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);

}
Run Code Online (Sandbox Code Playgroud)

这个正确复制文件并正确删除原始的Linux执行.

我认为这样做我需要使用类加载器调用类.