java.nio.file.FileSystemException:进程无法访问该文件,因为它正被另一个进程使用 [英] java.nio.file.FileSystemException: The process cannot access the file because it is being used by another process

查看:380
本文介绍了java.nio.file.FileSystemException:进程无法访问该文件,因为它正被另一个进程使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,在第一次执行时将它们自己复制到特定文件夹,在 linux 或 windows 中工作.
在 linux 中它工作得很好,但是当我尝试在 windows 上做同样的事情时,我收到以下错误:

I'm writing a program that copy themselve at first execution to a specific folder, working in linux or windows.
In linux it works perfectly but when I try to do the same on windows i get the following error:

java.nio.file.FileSystemException: 进程无法访问该文件,因为它正被另一个进程使用(在 sun.nio.fs.WindowsException 中)

java.nio.file.FileSystemException: The process cannot access the file because it is being used by another process (in sun.nio.fs.WindowsException)

那么,另一个进程就是程序本身,我应该用什么来跳过这个错误?

So, the other process is the program itself, what should I use to skip this error?

我的代码行是:

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

    }
} 

谢谢!

推荐答案

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

Ok, I don't found a perfect solution but something...

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 执行时正确擦除原始文件.

This copy correctly the file and erases correctly the original only on linux execution.

我认为要做到这一点,我需要使用类加载器调用该类..

I think for do that I need to invoke the class using a class loader..

这篇关于java.nio.file.FileSystemException:进程无法访问该文件,因为它正被另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆