-3

I have a p12 file upload function with the following code:

-

Then I want to create a function to delete the p12 file with the following code:

-

And when I run the result there is an error:

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

Is there a way for the file to be deleted successfully?

UPDATE : I've found the problem, apparently because the p12 file is used in this function:

-

Is there a way to still be able to delete the p12 file?

Haru Jess
  • 1
  • 1
  • 6

1 Answers1

0

The exception is telling you that there is another process that has the file open and therefore it can't be deleted. Take a look at your system processes (most probably applications) in Windows and see which application has the file open. Do you have the file open in a text editor such as notepad or in a command line shell? You have to close it there before it can be deleted.

You open the file

InputStream keyStoreStream = new FileInputStream(fileDir);

but the resource is never closed. Enclose the relevant parts in a try-with-resources block

Map<String, String> certSn;
try (InputStream keyStoreStream = new FileInputStream(fileDir)) {
    certSn =  = getP12Cert(keyStoreStream, passphrase.getPassphrase());
    // set up your assigneeModel here
} catch (IOException e) {
    // TODO throw or handle the exception however you need to
}

// rest of code here
geco17
  • 5,152
  • 3
  • 21
  • 38
  • I'm not opening anything. It looks like the file is being used in another function. But I don't know where. Is there a way to stop a running process? – Haru Jess Nov 17 '21 at 13:08
  • I've updated my question, I've included a function that requires its p12 file. Can you check and give me a solution. I would be very grateful. – Haru Jess Nov 17 '21 at 13:30
  • 1
    I'm glad it helped you but why did you delete all the code you posted? The question could be useful to other people. – geco17 Nov 17 '21 at 17:02