List resources from inside package..
Hi all,
I am simply trying to list the names of the resources within a package.
Here is my non-working code:
publicstaticvoid listResources(){
try{
System.out.println("listing");
Enumeration e = DocumentLoader.class.getClassLoader().getResources(("documentation/loader/docs/*"));
while(e.hasMoreElements()){
System.out.println(e.nextElement());
}
}catch(Exception ex){ex.printStackTrace();}
}
This code returns nothing.. Apparently because wildcards cannot be used in this manner...
Does anyone know how to use wildcards with this method, or some other method that works?
advTHANKSance
By the way, I cannot use File.list() because this code will eventually be in a jar... In fact it will eventurally be in an OSGi bundle...
here is a generic version of the code:
public static void listResources(){
try{
System.out.println("listing");
Enumeration e = some.SomeClass.class.getClassLoader().getResources(("some/package/*"));
while(e.hasMoreElements()){
System.out.println(e.nextElement());
}
}catch(Exception ex){ex.printStackTrace();}
}
There has got to be some simple way to list resources within a package... In the past I have included JarFiles in my distro and used jarfile.entries() to get lists of resources... But in this scenario, I do not want to include a second jar file...
Any ideas?
Thanks a lot for the quick response Kaj!
My work around was to put the resources in some package, but in a jar file.. Now I used JarInputStream to load the resources retrieving the names using jarentry.getName();
here is the code:
public static List getDocList() {
ArrayList docList = new ArrayList();
// get input stream to jar file
InputStream is = DocumentLoader.class.getClassLoader().getResourceAsStream("documentation/loader/docs/Docs.jar");
try{
JarInputStream jis = new JarInputStream(is);
// get entries
JarEntry entry = null;
while((entry = (JarEntry)jis.getNextJarEntry()) != null){
System.out.println("Jar entry:: " + entry.getName());
docList.add(entry.getName());
}
}catch(IOException e){e.printStackTrace();}
return docList;
}
Resource Loading with OSGi can be tricky ;)
Regards,
-BL
> My work around was to put the resources in some
> package, but in a jar file.. Now I used
> JarInputStream to load the resources retrieving the
> names using jarentry.getName();
That's great as long as you know about the limitations. A package doesn't need to be in one jar only (and that is why it's impossible to list all resources within a package, classes / jars can even be loaded dynamically from remote locations). Several jars can contain the same package so you are actually only listing those resources that are within one package in one jar file.
Kaj
Kaj