简体   繁体   English

如何在jar文件中使用ClassLoader.getResources()

[英]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. getResourcegetResources是在类加载器的类路径中查找命名资源,而不是列出目录或文件夹下的值。

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.

相关问题 ClassLoader.getResources()返回空枚举 - ClassLoader.getResources() returns empty enumeration ClassLoader.getResources 在 Weblogic/Spring 应用程序上返回一个空的枚举 - ClassLoader.getResources returning an empty Enumeration on a Weblogic/Spring application java jre 7u45中断了classloader.getResources()? - java jre 7u45 breaks classloader.getResources()? Java空路径约定,尤指在ClassLoader.getResources中使用的约定 - Java empty path convention, especially that used in ClassLoader.getResources 迭代ClassLoader.getResources()的结果时出现奇怪的NPE - Strange NPE when iterating over result of ClassLoader.getResources() 如何实现java.lang.Classloader getResources()? - how to implement java.lang.Classloader getResources()? 如何使用类加载器在model.zip文件上获取输入流,其中zip文件包装在classpath中的.jar文件中 - How do I use a classloader to get inputstream on a model.zip file where the zip file is wrapped in a .jar file in classpath jar文件中的Java classLoader - Java classLoader within a jar file 如何确保类加载器可以访问临时jar文件 - how to make sure that the temporary jar file is accessible by the classloader 如何使用 ClassLoader 从另一个 jar 加载 bean XML 文件 - How to load the bean XML file from another jar using ClassLoader
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM