java - return class names not working in Jar -
i have used code list of class names package:
private list<string> getclasses() { list<string> classes = new arraylist<string>(); string packagename = "algorithm/impl"; url directoryurl = thread.currentthread().getcontextclassloader(). getresource(packagename); file directory = new file(directoryurl.getfile()); if(directory.exists()) { string [] files = directory.list(); for(string filename : files) { classes.add(filename.substring(0, filename.lastindexof("."))); } } return classes; }
but not work when app packaged executable jar file. why?
you can make use of class jarfile.
jarfile file = new jarfile("yourfilename.jar"); (enumeration<jarentry> enum = file.entries(); enum.hasmoreelements();) { jarentry entry = enum.next(); system.out.println(entry.getname()); }
or if want search particular class inside jar can use zipfile class.
jarfile jar = new jarfile(yourjarfile); zipentry e = jar.getentry(class_file_to_find); if (e == null) { e = jar.getjarentry(class_file_to_find); if (e != null) { foundin.add(f.getpath()); } } else { foundin.add(f.getpath()); }
Comments
Post a Comment