How to know if a file is closed using java Process

307 Views Asked by At

I'm executing this command in order to open the log file with default file viewer:

Process p = Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler F:/Download/MSI3ca79.LOG");

I would like to know when the file is closed. Is it possible?

p.waitFor(); // doesn't work because the process is terminated just after the execution.

Solution:

ProcessBuilder pb = 
new ProcessBuilder("c:/windows/notepad.exe", "F:/Download/MSI3ca79.LOG");
File log = new File(LogFactory.getLogFactory(TestExternProcess.class).getName()); pb.redirectErrorStream(true);
pb.redirectOutput(Redirect.appendTo(log));
Process p = pb.start();
int exitVal = p.waitFor();
1

There are 1 best solutions below

1
HoRn On

Plainly, it's not possible. I would not go this way.

It depends on a particular app (Notepad) opening file. The system doesn't know in general when an app stops viewing a file. Because what is closing a file? Is it closing a tab in a viewer UI? Or cleaning memory allocated for the file by an app? Removing lock file in case of closing doc, xls files?

In order to do it, you would need to write a program controlling the OS and any app that can view the file.