How can I tell what processes are using a file under Java in Windows?

file, java, process, windows

Solution

I asked a similar question on Github for the JNA project and @matthiasblaesing wrote a code to accomplish this.

The code is here.

It prints a list with all the running processes in the format: `<exe>: <type>: <path>`.

Problem

We have a scheduled task written in Java that is failing on Windows platforms because sometimes the files it needs to delete are still in use. Is there a way from within Java that I can see what processes are using a file and get information on them? I thought I would add that I am willing to use JNA or JNI if necessary, and I assume it is going to be.

Original source