[英]How to use ClassLoader.getResources() in jar file
Problem statement: 问题陈述:
I have a jar file with a set of configuration files in a package mycompany/configuration/file/
. 我在包
mycompany/configuration/file/
有一个带有一组配置文件的jar文件。
I don't know the file names. 我不知道文件名。
My intention is to load the file names at runtime from the jar file in which they are packaged and then use it in my application. 我的目的是在运行时从打包它们的jar文件中加载文件名,然后在我的应用程序中使用它。
As far as I understood: When I use the ClassLoader.getResources("mycompany/configuration/file/")
I should be getting all the configuration files as URLs. 据我所知:当我使用
ClassLoader.getResources("mycompany/configuration/file/")
我应该将所有配置文件作为URL。
However, this is what is happening: 然而,这就是发生的事情:
I get one URL object with URL like jar:file:/C:/myJarName.jar!mycompany/configuration/file/
我得到一个带URL的URL对象,如
jar:file:/C:/myJarName.jar!mycompany/configuration/file/
Could you please let me know what I am doing wrong ? 能不能让我知道我做错了什么?
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. getResource
和getResources
是在类加载器的类路径中查找命名资源,而不是列出目录或文件夹下的值。
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). 因此,例如
ClassLoader.getResources("mycompany/configuration/file/Config.cfg")
将返回名为Config.cfg的所有文件,这些文件存在于类加载器的类路径中的mycompany / configuration / file路径中(我发现这特别有用)用于个人加载版本信息)。
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).
您获取的URL包含源jar文件(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/".
您可以使用此信息来破解打开jar文件,读取条目列表,过滤名称以“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) 从那里,您可以回退到
getResource
方法以加载对每个方法的引用(现在您有了名称和路径)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.