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

