javajunitjvmdelete-file

How can I ask the JVM if a file has been marked for deletion via File.deleteOnExit()?


I have Java code that marks files for deletion via File.deleteOnExit(). It works fine. I have a JUnit test setup to check if these files have been deleted. Of course this check fails because the unit test JVM has not terminated yet when the check is performed.

Is there a way a ask if these files are slated for deletion by the JVM?


Solution

  • Man, if I had just waited a few moments ...

    This works:

    Field field = Class.forName("java.io.DeleteOnExitHook").getDeclaredField("files");
    field.setAccessible(true);
    @SuppressWarnings("unchecked")
    LinkedHashSet<String> files = (LinkedHashSet<String>)field.get(null);
    

    I can then check the set and see if my files are contained therein.