selan
selan

Reputation: 1255

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

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: 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?

My code lines are:

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

    }
} 

Thanks!

Upvotes: 18

Views: 42678

Answers (1)

selan
selan

Reputation: 1255

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

}

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..

Upvotes: 10

Related Questions