using getResource given a directory in a jar

I packed my main class using one-jar, with main.jar as the jar w/ my classes i wrote.. here is the problem, i want to give the method getClasses a directory, and want to have it save the class name into a hashmap.. when i send

pckgname = com.dceg.rules

i get null for

Enumeration<URL> resources = cld.getResources(path);

how can i solve this problem.. ?

i was asked not to have the jar read itself... such as using java.util.jar.JarFile...

im thinking if i need to change the pckgname i send it.. to something like main.com.dceg.rules.. or would that also not work?

Thankyou so much!

heres the code and the paths within the jar

publicvoid getClasses(String pckgname)throws ClassNotFoundException{

// This will hold a list of directories matching the pckgname. There may be more than one if a package is split over multiple jars/paths

ArrayList<File> directories =new ArrayList<File>();

try{

ClassLoader cld = Thread.currentThread().getContextClassLoader();

if (cld ==null){

logger.error(new ClassNotFoundException("Can't get class loader."));

}

String path = pckgname.replace('.','/');

// Ask for all resources for the path

Enumeration<URL> resources = cld.getResources(path);

System.out.println("resource :"+resources.hasMoreElements());

while (resources.hasMoreElements()){

directories.add(new File(URLDecoder.decode(resources.nextElement().getPath(),"UTF-8")));

}

}catch (NullPointerException x){

logger.error(new ClassNotFoundException(pckgname +" does not appear to be a valid package (Null pointer exception)"));

}catch (UnsupportedEncodingException encex){

logger.error(new ClassNotFoundException(pckgname +" does not appear to be a valid package (Unsupported encoding)"));

}catch (IOException ioex){

logger.error(new ClassNotFoundException("IOException was thrown when trying to get all resources for " + pckgname));

}

// For every directory identified capture all the .class files

for (File directory : directories){

if (directory.exists()){

// Get the list of the files contained in the package

String[] files = directory.list();

for (String file : files){

// we are only interested in .class files

if (file.endsWith(".class")){

// removes the .class extension

classes.put( file.substring(0, file.length() - 6) , (pckgname +'.' + file.substring(0, file.length() - 6)) );

}

}

}else{

thrownew ClassNotFoundException(pckgname +" (" + directory.getPath() +") does not appear to be a valid package");

}

}

}

Valid.jar:

META-INF/MANIFEST.MF

com/

com/simontuffs/

com/simontuffs/onejar/

com/simontuffs/onejar/Boot.class

com/simontuffs/onejar/Handler$1.class

com/simontuffs/onejar/Handler.class

com/simontuffs/onejar/JarClassLoader$1.class

com/simontuffs/onejar/JarClassLoader$ByteCode.class

com/simontuffs/onejar/JarClassLoader.class

com/simontuffs/onejar/services/

com/simontuffs/onejar/services/english/

com/simontuffs/onejar/services/english/HelloService$1.class

com/simontuffs/onejar/services/english/HelloService.class

com/simontuffs/onejar/services/french/

com/simontuffs/onejar/services/french/HelloService$1.class

com/simontuffs/onejar/services/french/HelloService.class

com/simontuffs/onejar/services/german/

com/simontuffs/onejar/services/german/HelloService$1.class

com/simontuffs/onejar/services/german/HelloService.class

com/simontuffs/onejar/services/IHello.class

com/simontuffs/onejar/services/IHelloService.class

doc

lib/

lib/dnsjava-2.0.2.zip

lib/jargs.jar

lib/log4j-1.2.13.jar

main/

main/main.jar

META-INF/

main.jar:

META-INF/MANIFEST.MF

com/

com/dceg/

com/dceg/rules/

com/dceg/rules/RuleBase.class

com/dceg/rules/RuleBase.java

com/dceg/rules/Validator.class

com/dceg/rules/Validator.java

com/dceg/rules/ValidHostname.class

com/dceg/rules/ValidHostname.java

com/dceg/rules/ValidURL.class

com/dceg/rules/ValidURL.java

[6236 byte] By [hamham3001a] at [2007-10-3 2:43:21]
# 1
Haven't we just been through this? Directories, whether in or out of jars, are not resources.
malcolmmca at 2007-7-14 20:31:42 > top of Java-index,Java Essentials,Java Programming...
# 2
but how is it that i can get it to work in eclispe?since i got this class from googling it, im not exactly sure how it works..
hamham3001a at 2007-7-14 20:31:42 > top of Java-index,Java Essentials,Java Programming...
# 3

> but how is it that i can get it to work in eclispe?

> since i got this class from googling it, im not

> exactly sure how it works..

The ClassLoader system isn't written to work on code sources where contents listing is possible, for example it ttreast an HTTP code source exactly as a class directory or a jar file.

So if you really need to list the contents of a file directory tree or a jar file you need to treat them as files. Mind you it's usually a sign of bad design to search class directories anyway.

However, if you must, you can get a list of entries from a .jar with the java.util.jar.JarFile class. Then just check whether the path name returned starts with the appropriate directory path.

malcolmmca at 2007-7-14 20:31:42 > top of Java-index,Java Essentials,Java Programming...
# 4
i see.. thank you, now i think i understand more about classloader..
hamham3001a at 2007-7-14 20:31:42 > top of Java-index,Java Essentials,Java Programming...