java.nio.文件.FileSystemException : The process cannot access the file because it is being used by another process

标签 java windows

我正在编写一个程序,在第一次执行时将自己复制到特定文件夹,在 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/

相关文章:

java - 匿名线程类的输出不同步

java - 证书链验证

c++ - 在 Windows 7 上开始使用 OpenCV 2.4 和 MinGW

Java 代码在处理器速度较慢的 Mac 上运行速度比在 Windows 计算机上运行速度更快?

.net - 现在我的本地主机运行速度很慢,因为我在 Windows 7 和 Asp.net MVC 上

java - Eclipse TreeViewer/ViewPart 后台点击

java - while 循环问题

java - 在rabbitmq消息中添加额外的字符

windows - 非交互式 "git clean -fdx"

以相反顺序显示行的 Windows 日志文件查看器(尾部)