fix: DirectoryStream not closed

dc31674721 (r53347194)
This commit is contained in:
Haowei Wen 2021-07-12 13:19:16 +08:00
parent 46a7269087
commit e7d5faeeac
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4

View File

@ -9,6 +9,7 @@ import org.jackhuang.hmcl.util.platform.JavaVersion;
import org.jackhuang.hmcl.util.platform.OperatingSystem; import org.jackhuang.hmcl.util.platform.OperatingSystem;
import java.io.IOException; import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Optional; import java.util.Optional;
@ -45,12 +46,14 @@ public final class JavaRepository {
String platform = platformOptional.get(); String platform = platformOptional.get();
Path javaStoragePath = getJavaStoragePath(); Path javaStoragePath = getJavaStoragePath();
if (Files.isDirectory(javaStoragePath)) { if (Files.isDirectory(javaStoragePath)) {
for (Path component : Files.newDirectoryStream(javaStoragePath)) { try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(javaStoragePath)) {
Path javaHome = component.resolve(platform).resolve(component.getFileName()); for (Path component : dirStream) {
try { Path javaHome = component.resolve(platform).resolve(component.getFileName());
addJava(javaHome); try {
} catch (IOException e) { addJava(javaHome);
LOG.log(Level.WARNING, "Failed to determine Java at " + javaHome, e); } catch (IOException e) {
LOG.log(Level.WARNING, "Failed to determine Java at " + javaHome, e);
}
} }
} }
} }