<div dir="ltr"><div dir="ltr">On Tue, Sep 24, 2024 at 1:44 PM Eirik Bjørsnøs <<a href="mailto:eirbjo@gmail.com">eirbjo@gmail.com</a>> wrote:</div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div>Does the class loader in question return JAR-form URLs? If that's the case, you could use JarURLConnection.getJarFile() and use getInputStream to get the versioned resource of choice:</div></div></div></blockquote><div><br></div><div>If you also want to offload the version lookup logic to JarFile, re-open the JarFile with the desired runtime version,  like in the following test:</div><div><br></div><div><font face="monospace">@Test<br>public void readVersionedResourceFromClassLoader() throws IOException {<br>    // Base name of the class file resource looked up in this test<br>    String baseName = "com/example/HelloWorld.class";<br><br>    // Create a multi-release JAR file including a versioned entry<br>    Path zip = createMultiReleaseJar(baseName);<br>    <br>    // A class loader backed by the multi-release JAR file<br>    ClassLoader loader = new URLClassLoader(new URL[] {zip.toUri().toURL()});<br><br>    // The version to get resources for<br>    Runtime.Version version = Runtime.Version.parse("17");<br><br>    URL resource = loader.getResource(baseName);<br><br>    if (resource != null && resource.openConnection() instanceof JarURLConnection con) {<br>        File file = new File(con.getJarFile().getName());<br>        // Open a JarFile for the desired runtime version<br>        try (JarFile jf = new JarFile(file, true, ZipFile.OPEN_READ, version)) {<br>            JarEntry entry = jf.getJarEntry(baseName);<br>            assertEquals(baseName, entry.getName());<br>            assertEquals("META-INF/versions/11/" + baseName, entry.getRealName());<br>            try (var is = jf.getInputStream(entry)) {<br>                assertEquals("11", new String(is.readAllBytes()));<br>            }<br>        }<br>    }<br>}<br><br>private static Path createMultiReleaseJar(String baseName) throws IOException {<br>    Path zip = Path.of("multi-release.zip");<br>    Manifest man = new Manifest();<br>    Attributes attrs = man.getMainAttributes();<br>    attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");<br>    attrs.put(Attributes.Name.MULTI_RELEASE, "true");<br>    try (JarOutputStream jo = new JarOutputStream(Files.newOutputStream(zip), man)) {<br>        jo.putNextEntry(new ZipEntry("META-INF/versions/11/" + baseName));<br>        jo.write("11".getBytes(StandardCharsets.UTF_8));<br>        jo.putNextEntry(new ZipEntry(baseName));<br>        jo.write("base".getBytes(StandardCharsets.UTF_8));<br>    }<br>    return zip;<br>}</font><br></div><div><br></div><div><br></div><div> </div></div></div>