java 如何遍历Java类资源?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/749533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:36:59  来源:igfitidea点击:

How to walk through Java class resources?

javaresourcesclasspath

提问by David Hofmann

I know we can do something like this:

我知道我们可以做这样的事情:

Class.class.getResourceAsStream("/com/youcompany/yourapp/module/someresource.conf")

to read the files that are packaged within our jar file.

读取打包在我们的 jar 文件中的文件。

I have googled it a lot and I am surely not using the proper terms; what I want to do is to list the available resources, something like this:

我在谷歌上搜索了很多,我肯定没有使用正确的术语;我想要做的是列出可用资源,如下所示:

Class.class.listResources("/com/yourcompany/yourapp")

That should return a list of resources that are inside the package com.yourcompany.yourapp.*

这应该返回包内的资源列表 com.yourcompany.yourapp.*

Is that possible? Any ideas on how to do it in case it can't be done as easily as I showed?

那可能吗?关于如何做到这一点的任何想法,以防它不能像我展示的那样轻松完成?

Note: I know it is possible to know where your jar is and then open it and inspect its contents to achieve it. But, I can't do it in the environment I am working in now.

注意:我知道可以知道您的 jar 在哪里,然后打开它并检查其内容以实现它。但是,在我现在工作的环境中,我做不到。

采纳答案by erickson

For resources in a JAR file, something like this works:

对于 JAR 文件中的资源,如下所示:

URL url = MyClass.class.getResource("MyClass.class");
String scheme = url.getProtocol();
if (!"jar".equals(scheme))
  throw new IllegalArgumentException("Unsupported scheme: " + scheme);
JarURLConnection con = (JarURLConnection) url.openConnection();
JarFile archive = con.getJarFile();
/* Search for the entries you care about. */
Enumeration<JarEntry> entries = archive.entries();
while (entries.hasMoreElements()) {
  JarEntry entry = entries.nextElement();
  if (entry.getName().startsWith("com/y/app/")) {
    ...
  }
}

You can do the same thing with resources "exploded" on the file system, or in many other repositories, but it's not quite as easy. You need specific code for each URL scheme you want to support.

您可以对文件系统或许多其他存储库中“爆炸”的资源执行相同的操作,但这并不那么容易。对于要支持的每个 URL 方案,您都需要特定的代码。

回答by Jon Skeet

In general can't get a list of resources like this. Some classloaders may not even be ableto support this - imagine a classloader which can fetch individual files from a web server, but the web server doesn't have to support listing the contents of a directory. For a jar file you can load the contents of the jar file explicitly, of course.

通常无法获得这样的资源列表。一些类加载器甚至可能无法支持这一点——想象一个可以从 Web 服务器获取单个文件的类加载器,但 Web 服务器不必支持列出目录的内容。当然,对于 jar 文件,您可以显式加载 jar 文件的内容。

(This questionis similar, btw.)

这个问题很相似,顺便说一句。)

回答by paweloque

I've been looking for a way to list the contents of a jar file using the classloaders, but unfortunately this seems to be impossible. Instead what you can do is open the jar as a zip file and get the contents this way. You can use standard (here) ways to read the contents of a jar file and then use the classloader to read the contents.

我一直在寻找一种使用类加载器列出 jar 文件内容的方法,但不幸的是,这似乎是不可能的。相反,您可以做的是将 jar 作为 zip 文件打开并以这种方式获取内容。您可以使用标准(此处)方法读取 jar 文件的内容,然后使用类加载器读取内容。

回答by Luke Hutchison

The most robust mechanism for listing all resources in the classpath is currently to use this pattern with ClassGraph, because it handles the widest possible array of classpath specification mechanisms, including the new JPMS module system. (I am the author of ClassGraph.)

列出类路径中所有资源的最强大的机制目前是将此模式与 ClassGraph 一起使用,因为它可以处理最广泛的类路径规范机制,包括新的 JPMS 模块系统。(我是 ClassGraph 的作者。)

List<String> resourceNames;
try (ScanResult scanResult = new ClassGraph()
            .whitelistPaths("com/yourcompany/yourapp")
            .scan()) {
    resourceNames = scanResult.getAllResources().getNames();
}

回答by Vladimir

I usually use

我通常使用

getClass().getClassLoader().getResourceAsStream(...)

but I doubt you can list the entries from the classpath, without knowing them a priori.

但我怀疑您是否可以列出类路径中的条目,而无需事先了解它们。