Skip to content

Commit 9c9ac28

Browse files
committed
Work around Windows limitation in createTemporaryDirectory()
Windows really does not let you delete files that are in use, even if "in use" really means that there is a ClassLoader referencing that file and that ClassLoader just happens not to be garbage-collected yet. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 678e9fa commit 9c9ac28

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/main/java/org/scijava/test/TestUtils.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,15 @@ public static File createTemporaryDirectory(final String prefix,
120120
baseDirectory = new File(path);
121121
}
122122

123-
final File file = new File(baseDirectory, prefix + suffix);
124-
if (file.isDirectory()) FileUtils.deleteRecursively(file);
123+
File file = new File(baseDirectory, prefix + suffix);
124+
if (file.isDirectory()) {
125+
if (!FileUtils.deleteRecursively(file)) {
126+
// Oh, how I *love* Windows. Love, love, love.
127+
for (int i = -1; file.isDirectory(); i--) {
128+
file = new File(baseDirectory, prefix + i + suffix);
129+
}
130+
}
131+
}
125132
else if (file.exists() && !file.delete()) {
126133
throw new IOException("Could not remove " + file);
127134
}

0 commit comments

Comments
 (0)