I am using reflection to find all the directories in a certain package, and then returning those directories as java.io.Files so that I can extract class files from them. My method for getting all the directories in a package is this:
private static List<File> findDirs(String packageName) throws IOException,
UnsupportedEncodingException, URISyntaxException {
List<File> result = new ArrayList<File>();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
assert classLoader != null;
String path = packageName.replace('.', '/');
Enumeration<URL> resources = classLoader.getResources(path);
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
URI uri = resource.toURI();
File f = new File(uri);// this line throws the exception
result.add(f);
}
return result;
}
It works perfectly fine when running my application from the source code. However, Once I deploy it,and run it, the method findDirs fails, as the File constructor throws an IllegalArgumentsException"URI is not Hierarchical".
I have debuged the code, and the URI in question which is causing the problem is:
jar:file:/C:/Program%20Files/apache-activemq-5.5.1/bin/../lib/App.jar!/com/demo/payload
Whereas I have no problem with the URI when running from source:
file:/C:/Marcus/JavaProjects/App/build/prod/classes/com/demo/payload
How can I create a file pointing to the payload directory within the App.jar from a non-herarchical URI such as the one mentioned?
I can get as far as creating a JarFile that points to the App.jar itself using JarURLConnection, and can then get a JarEntry pointing to the directory I'm searching for, but can't seem to convert this entry to a file...