Can I list the resources in a given package?

£可爱£侵袭症+ 提交于 2019-11-26 23:27:58

问题


Lets assume my classes are loaded from a compressed .war file or loaded elsewhere, how can I discover all the resources in a given package? Enumerating files will not really work, since this is a war file. Most likely this will involve using the current classloader?

Is there a library out there that does something like that? Googling revealed only some hacks with listing files.


回答1:


No, you can't list resources in a package because the JVM simply doesn't know what resources are in what package. See this question.




回答2:


Yes. You can use ClassLoader.getResources("");

Or you could use ferret API:
https://www.ohloh.net/p/pureperfect-ferret

This API allows you to scan the class path as well as arbitrary archives and directories using the Visitor pattern.




回答3:


The Spring Framework offers a class called PathMatchingResourcePatternResolver. With this class you can give Ant style paths with wildcards and it will find the relevant resources for you (searching through the folders and jars on the classpath).




回答4:


I do this: - before packaging, ls myResources/*.ext > myResources/list.txt - then just read the file first



来源:https://stackoverflow.com/questions/186496/can-i-list-the-resources-in-a-given-package

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