getResourceAsStream()可以在jar文件外找到文件吗? [英] Can getResourceAsStream() find files outside the jar file?
问题描述
我正在开发一个使用库来加载配置文件的应用程序:
I'm developing an application that uses a library that loads a configuration file as:
InputStream in = getClass().getResourceAsStream(resource);
我的应用程序然后打包在 .jar
文件。如果 resource
在 .jar
文件中,我可以将路径指定为/ relative /路径/到/资源/资源
。
My application in then packed in a .jar
file. If resource
is inside the .jar
file, I can specify the path as "/relative/path/to/resource/resource"
.
我想知道是否可以找到资源,如果它在 .jar
文件之外,在这种情况下,我将如何指定路径。
I'd like to know if is it possible to find the resource if it is outside the .jar
file, and in this case, how would I specify the path.
(假设我的应用程序的jar在 app /
资源在 app / config
)。
(Assuming my application's jar is in app/
and the resource is in app/config
).
程序使用3 rd 方库。
库将资源用作配置文件。
The program uses a 3rd party library. The library uses the resource as a configuration file.
我还想调整配置文件,而不必一直解压缩/压缩jar文件。
I also want to tweak the configuration file without having to unzip/zip the jar file all the time.
推荐答案
一般情况下,是的。从技术上讲,类的 ClassLoader
用于定位参数中指定的资源(请参阅此处的Javadoc )。如果您没有使用特殊的 ClassLoader
,那么您将获得引导类加载器,它会搜索类路径。因此,如果您在类路径中有目录或其他jar文件,则会搜索它们。
In general, yes it can. Technically, the class's ClassLoader
is used to locate the resource named in the parameter (see Javadoc here). If you're not using a special ClassLoader
then you'll get the bootstrap class loader, which searches the class path. So, if you have directories or other jar files on the classpath, they will be searched.
这篇关于getResourceAsStream()可以在jar文件外找到文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!