Java Reference
In-Depth Information
final
final String packageAsDirName = packageName . replace ( "." , "/" );
final
final List < String > list = new
new ArrayList <>();
final
final Enumeration < URL > urls =
Thread . currentThread ().
getContextClassLoader ().
getResources ( packageAsDirName );
while
while ( urls . hasMoreElements ()) {
URL url = urls . nextElement ();
// System.out.println("URL = " + url);
String file = url . getFile ();
switch
switch ( url . getProtocol ()) {
case
case "file" :
// This is the easy case: "file" is
// the full path to the classpath directory
File dir = new
new File ( file );
for
for ( File f : dir . listFiles ()) {
list . add ( packageAsDirName + "/" + f . getName ());
}
break
break ;
case
case "jar" :
// This is the harder case; "file" is of the form
// "jar:/home/ian/bleah/darwinsys.jar!com/darwinsys/io"
// for some jar file that contains at least one class from
// the given package.
int
int colon = file . indexOf ( ':' );
int
int bang = file . indexOf ( '!' );
String jarFileName = file . substring ( colon + 1 , bang );
JarFile jarFile = new
new JarFile ( jarFileName );
Enumeration < JarEntry > entries = jarFile . entries ();
while
while ( entries . hasMoreElements ()) {
JarEntry e = entries . nextElement ();
String jarEntryName = e . getName ();
iif (! jarEntryName . endsWith ( "/" ) &&
jarEntryName . startsWith ( packageAsDirName )) {
list . add ( jarEntryName );
}
}
break
break ;
default
default :
throw
throw new
new IllegalStateException (
"Dunno what to do with URL " + url );
}
}
return
return list . toArray ( new
new String [] {});
}
Search WWH ::




Custom Search