Skip to content

Commit ed39770

Browse files
committed
Fix JAR file loading bug
1 parent 34ba115 commit ed39770

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

swan-pipeline/src/main/java/de/fraunhofer/iem/swan/cli/FileUtility.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,34 +58,35 @@ public File getResourceDirectory(String resourceLocation) throws IOException {
5858

5959
// Find JAR Entries
6060
URL url = FileUtility.class.getResource(resourceLocation);
61-
if (resourceLocation.startsWith(File.pathSeparator)) {
61+
if (resourceLocation.startsWith(File.separator)) {
6262
resourceLocation = resourceLocation.substring(1);
6363
}
6464

65+
assert url != null;
6566
if (url.getProtocol().equals("jar")) {
66-
String[] splitted = url.getPath().split("\\!");
67-
splitted = splitted[0].split("file:/");
68-
String jarPath = splitted[splitted.length - 1];
69-
File jarFile = new File(jarPath);
67+
68+
String[] jarFilePath = url.getPath().split("!")[0].split("file:");
69+
70+
File jarFile = new File(jarFilePath[jarFilePath.length - 1]);
7071

7172
if (jarFile.exists()) {
72-
JarFile jar = null;
73-
try {
74-
jar = new JarFile(jarFile.getAbsolutePath());
73+
74+
try (JarFile jar = new JarFile(jarFile)) {
75+
7576
Enumeration<JarEntry> entries = jar.entries(); // gives ALL entries in jar
77+
7678
while (entries.hasMoreElements()) {
77-
String name = entries.nextElement().getName();
78-
if (name.startsWith(resourceLocation) && !name.endsWith("/")) {
79-
String destinationName = name.substring(resourceLocation.length());
79+
80+
JarEntry entry = entries.nextElement();
81+
82+
if (entry.getName().contains(resourceLocation) && !entry.getName().endsWith(File.separator)) {
83+
84+
String destinationName = entry.getName().substring(resourceLocation.length());
8085
File destination = new File(tempDir, destinationName);
81-
InputStream stream = FileUtility.class.getResourceAsStream("/" + name);
86+
InputStream stream = FileUtility.class.getResourceAsStream(File.separator + entry.getName());
8287
FileUtils.copyInputStreamToFile(stream, destination);
8388
}
8489
}
85-
} finally {
86-
if (jar != null) {
87-
jar.close();
88-
}
8990
}
9091
}
9192
}

0 commit comments

Comments
 (0)