URLClassLoader.findResources returns incomplete enumeration when using JarIndex
杨易(青风)
qingfeng.yy at alibaba-inc.com
Tue Aug 4 09:09:20 UTC 2020
Hi,
When I use URLClassLoader.findResources to find given resource from many jar files, it works as I expected, but as I generate jar index(jar -i) for jar files, URLClassLoader.findResources will return only ONE resource file even if there exist multi resource files. I can reproduce this scenario by the following script with JDK11~14
```bash
#! /bin/bash
if [ ! -d "bug_ucp" ]; then
mkdir bug_ucp
fi
cd bug_ucp
echo '
public class A{
public A(){}
void foo(){}
}
' > A.java
javac A.java
touch res1
touch res2
jar cf A.jar A.class res2
jar cf B.jar A.class res1 res2
jar cf X.jar res1 res2
PREFIX=`pwd`
ajar=$PREFIX/"A.jar"
bjar=$PREFIX/"B.jar"
xjar=$PREFIX/"x.jar"
echo '
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
public class FindSameRes {
private static URL[] urls;
private static URLClassLoader loader;
static {
try {
urls = new URL[]{
new URL("file:'$xjar'"),
new URL("file:'$ajar'"),
new URL("file:'$bjar'")
};
loader = new URLClassLoader(urls);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public static void findMultiResource() {
try {
Enumeration<URL> result = loader.findResources("res2");
while (result.hasMoreElements()){
System.out.println(result.nextElement());
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
findMultiResource();
}
}
' > FindSameRes.java
javac FindSameRes.java
echo 'Find res in jars, without JarIndex'
java FindSameRes
echo 'Find res in jars, with JarIndex'
jar -i $xjar $ajar $bjarjava FindSameRes
```
Is this a bug?
More information about the jdk-updates-dev
mailing list