Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 518
skiphoppy Avatar asked Jun 19 '12 16:06

skiphoppy


2 Answers

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

like image 157
IvanRF Avatar answered Nov 01 '22 22:11

IvanRF


One dirty way to do it is to run your application with administrative privileges and invoke a command utility such as Handle using a ProcessBuilder.

like image 37
Anthony Accioly Avatar answered Nov 01 '22 22:11

Anthony Accioly