Maven包打开jar包内的文件(作为*File*)
我需要打开一个 Maven jar 包内的文件。它是我使用的框架的模型配置,库类的构造函数需要传递 File 类型的对象。我可以使用类加载器获取配置文件的路径,没有任何问题。但是 - 不幸的是 - File 无法读取 jar 内的文件。所以我得到了java.io.FileNotFoundException。现在我正在寻找这个问题的解决方案。我的计划是解压模型配置文件并将其放在临时目录中。然而,在开始编码之前,我想了解是否有其他解决方案来解决像我这样的问题。
更新:我需要在运行时读取文件。
I need to open a file which is inside a maven jar package. It a model configuration for a framework I use and a constructor of a library class requires to pass a object of type File. I can get a path to a configuration file using class loader without any problems. But -- unfortunately -- File can not read a file inside jar. So I get java.io.FileNotFoundException. Now I looking for a solution for this problem. My plan is to decompress the model configuration file and place it in a temporary directory. However, before starting coding, I would like to learn if there is any other solution for such a problem like mine.
UPDATE: I need to read a file in runtime.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 jar 资源解压到文件中
dependency:unpack-dependency
(如果 jar 是项目的 Maven 依赖项之一)<前><代码><插件>;
org.apache.maven.plugins
maven-dependency-plugin ;
解包依赖项
the.groupId
the.artifactId
您想要它的位置
<处决>
<执行>
<阶段>生成资源
<目标>
<目标>解包依赖项
<配置>
<包含> **/path/to/your/resource.txt
或使用
dependency:unpack
(如果 jar 没有依赖项,但仍可作为 Maven 工件使用)<前><代码><插件>;
org.apache.maven.plugins
maven-dependency-plugin ;
解压
the.groupid ;
the.artifactid ;
您想要它的位置
<处决>
<执行>
<阶段>生成资源
<目标>
<目标>解包
<配置>
<工件项目>
<工件项目>
<版本>the.version
<类型>罐子
<包含> **/path/to/your/resource.txt
If you're doing it from a maven build, unpack the jar resource to a file using
dependency:unpack-dependencies
(if the jar is one of the project's maven dependenies)or use
dependency:unpack
(if the jar is no dependency, but still available as a maven artifact)我认为你应该使用 JarInputStream,逐一遍历 JAR 条目,直到找到您需要的内容。然后只需
read()
找到的JarEntry
内容即可。I think you should use JarInputStream, traversing through JAR entries one by one, until you find what you need. Then just
read()
content of the foundJarEntry
.