From fd91712f86536f5144844edf012a7ebbf19acfc5 Mon Sep 17 00:00:00 2001 From: jdstroy Date: Fri, 15 Sep 2023 20:10:33 -0500 Subject: [PATCH] Update image enumeration code for later JDK versions --- src/org/jpc/j2se/JPCApplication.java | 67 +++++++++++++++------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/src/org/jpc/j2se/JPCApplication.java b/src/org/jpc/j2se/JPCApplication.java index 926aba50f..4c6f31e81 100644 --- a/src/org/jpc/j2se/JPCApplication.java +++ b/src/org/jpc/j2se/JPCApplication.java @@ -589,41 +589,48 @@ private static final Iterator getResources(String directory) List resources = new ArrayList(); ClassLoader cl = JPCApplication.class.getClassLoader(); - if (!(cl instanceof URLClassLoader)) - throw new IllegalStateException(); - URL[] urls = ((URLClassLoader) cl).getURLs(); + + URL[] urls; + if (cl instanceof URLClassLoader) + { + URLClassLoader urlClassLoader = (URLClassLoader) cl; + urls = urlClassLoader.getURLs(); + } + else + { + urls = new URL[] { + JPCApplication.class.getProtectionDomain().getCodeSource().getLocation() + }; + } int slash = directory.lastIndexOf("/"); String dir = directory.substring(0, slash + 1); - for (int i=0; i= 0) - thisDir = name.substring(0, slash + 1); - - if (!dir.equals(thisDir)) - continue; - resources.add(name); - } - - jarStream.close(); } - catch (IOException e) { e.printStackTrace();} + try { + try (JarInputStream jarStream = new JarInputStream(url.openStream())) { + while (true) + { + ZipEntry entry = jarStream.getNextEntry(); + if (entry == null) + break; + if (entry.isDirectory()) + continue; + + String name = entry.getName(); + slash = name.lastIndexOf("/"); + String thisDir = ""; + if (slash >= 0) + thisDir = name.substring(0, slash + 1); + + if (!dir.equals(thisDir)) + continue; + resources.add(name); + } + } + }catch (IOException e) { e.printStackTrace();} } InputStream stream = context.getResourceAsStream(directory); try