jar problem: getResouces given a directory returning null
Hi, i am having problems with trying to get files from a directory inside the jar.
In my jar there is the following:
manifest.mf
.classpath
.project
com/dceg/rules/validator.java (main class)
com/dceg/rules/.... (other class files)
inside the main class, validator.java, i have a method which tries to read the files given a directory : com/dceg/rules, and save the filenames within the directory into a hashmap for later use.. but when i package it into a jar file, the line:
cld.getResources(path);
gives me null... where this isnt the case when i was running it on eclispe.. is there any solutions to this problem?
thank you so much!
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);
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");
}
}
}
Message was edited by:
hamham3001

