访问打包到jar文件中的文件 [英] Accessing files packaged into a jar file
问题描述
我创建了一个带有可运行编译类和一些模板文件(文本)的jar文件。我现在想要使用打包到jar中的文本文件在应用程序中作为 File
对象进行分发。
I created a jar file with a runnable compiled class and some template files (text). I now want to use the text files packaged into the jar for distribution in the application as File
objects.
I试图只使用不起作用的相对路径。 如何获取指向jar中文本文件的文件
对象?
I tried to just use relative paths which did not work. How do I get a File
object pointing to a text file which is in the jar?
有没有办法做到这一点,所以它在运行unpackaged类文件和从具有相同代码的jar时都可以工作,还是需要使用单独的函数?
Is there a way to do this so it will work both when running the unpackaged class files and from a jar with the same code, or will I need to use separate functions?
推荐答案
你可以像 Class.getResourceAsStream(String)
或类似内容。
from inside jar you can use it like Class.getResourceAsStream(String)
, or something similar.
从外面看它的文件不是它的jar所以你必须提取它以便实际使用内部文件
from out side its not the file its jar so you will have to extract it in order to actually use inside file
这篇关于访问打包到jar文件中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!