繁体   English   中英

如何在类路径中找到具有指定名称的所有资源?

[英]How to locate all resources in classpath with a specified name?

我想在 class 路径中列出所有具有特定名称的文件。 我预计会出现多次,因此Class.getResource(String)将不起作用。

基本上,我必须在 class 路径中的任何位置识别所有具有特定名称的文件(例如:xyz.properties),然后累积读取其中的元数据。

我想要一些效果Collection<URL> Class.getResources(String)但找不到类似的东西。

PS:我没有使用任何第三方库的奢侈,因此需要一个本土解决方案。

您可以在类加载器上使用Enumeration getResources(String name)来实现相同的功能。

例如:

Enumeration<URL> enumer = Thread.currentThread().getContextClassLoader().getResources("/Path/To/xyz.properties");
while (enumer.hasMoreElements()) {
    System.out.print(enumer.nextElement());
}

我所做的是从classpath读取java源文件并使用ClassLoader处理它们。 我正在使用以下代码:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

assert (classLoader != null);

// pkgName = "com.comp.pkg"
String path = pkgName.replace('.', '/');

// resources will contain all java files and sub-packages
Enumeration<URL> resources = classLoader.getResources(path);

 if(resources.hasMoreElements()) {
        URL resource = resources.nextElement();     
        File directory = new File(resource.getFile());
        // .. process file, check this directory for properties files
 }

希望这对你有所帮助。

建议使用ClassLoader.getResources(String)的现有答案仅在所讨论的“名称”是资源的完整路径时才有效。 正如方法的 javadoc 所述,“资源的名称是标识资源的/分隔的路径名。 ”不幸的是,参数被称为“名称”而不是“路径”是令人误解的。

对于那些像我一样真正在寻找具有给定(简单)名称的所有资源的人 - 不知道资源的完整路径 - 当前的答案将不起作用。 @NandkumarTekale 的代码示例在评论中声明“资源将包含所有 java 文件和子包”,但不幸的是,这不是真的。 Nor does it make any difference to use ClassLoader.findResources(String) (which is indeed protected in the ClassLoader class itself, but pulled into the public API by concrete subclasses like URLClassLoader , which in turn serves as the base class for most commonly used class装载机)。

我能找到的最直接的解决方案是使用ClassGraph库:

try (ScanResult result = new ClassGraph().acceptClasspathElementsContainingResourcePath("*/html5.dtd").scan())
{
    System.err.println(result.getResourcesWithLeafName("html5.dtd").getURLs());
}

可悲的是,这与 OP 对不使用第三方库的解决方案的要求相冲突。 然而,ClassGraph 的源代码可在 GitHub 上获得,并可用作“本土”解决方案的灵感。 In broad strokes, one would have to find the class loader's base URLs (eg, using URLClassLoader.getURLs() ) and then write URL-specific code for searching the contents of each URL (eg, if it's a jar:file: .. . URL, load the JAR and iterate over its contents, or if it's a file: ... URL use java.io.File to explore the folder contents). 不过,有很多特殊情况需要考虑(例如,OSGi class 加载程序),一个真正通用的解决方案可能会复制相当多的 ClassGraph 在后台所做的工作。 例如,如果假设资源将始终打包在 JAR 中(或始终作为文件系统中的普通文件)是有效的,则可以针对这些情况创建更定制的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM