How to use ClassLoader.getResources() in jar file

三世轮回 提交于 2019-12-01 20:37:39

For what you are trying to do I don't think it is possible.

getResource and getResources are about finding named resources within the classloader's classpath, not listing values beneath a directory or folder.

So for example ClassLoader.getResources("mycompany/configuration/file/Config.cfg") would return all the files named Config.cfg that existed in the mycompany/configuration/file path within the class loader's class path (I find this especially useful for loading version information personally).

In your case I think you might almost have half a solution. The URL you are getting back contains the source jar file (jar:file:/C:/myJarName.jar). You could use this information to crack open the jar file a read a listing of the entries, filtering those entries whose name starts with "mycompany/configuration/file/".

From there, you could then fall back on the getResource method to load a reference to each one (now that you have the name and path)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!