如何从war文件中打包的jar文件加载资源?
我需要从 jar 加载属性文件。该 jar 包含在 war 文件中。这是结构
ROOT.war
WEB-INF
lib
my.jar
here my.jar has following structure
my.jar
com
test
myservlet.class
WEB-INF
test.property
现在我在我的一个 servlet 中编写了以下代码,如下所示:
InputStream stream = getServletContext().getResourceAsStream("/WEB-INF/test.properties");
Properties prop = new Properties();
prop.load(stream );
但在上面的代码中我得到的流为空。如果我将属性文件放在 ROOT.war/WEB-INF 中,它就可以正常工作。我有一个合理的想法,如果 getResourceAsStream 中的路径以“/”开头,那么它会在上下文根中搜索资源。但是我如何读取位于根应用程序的 WEB-INF/lib 中再次找到的 jar 中的资源?
谢谢&问候, 阿米特·帕特尔
I need to load a property file from the jar. The jar is included in war file. Here is the structure
ROOT.war
WEB-INF
lib
my.jar
here my.jar has following structure
my.jar
com
test
myservlet.class
WEB-INF
test.property
Now I have written following code in one of my servlet as follows:
InputStream stream = getServletContext().getResourceAsStream("/WEB-INF/test.properties");
Properties prop = new Properties();
prop.load(stream );
but above code I got stream as null. If I put the property file in ROOT.war/WEB-INF it works fine. I have fair idea that if path in getResourceAsStream starts with '/' than it search resource in context root. But how can I read resource which lies in a jar which again found in WEB-INF/lib of root application?
Thanks & Regards,
Amit Patel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将其放在 JAR 的根目录中,并通过上下文类加载器而不是 servletcontext 获取它。
/WEB-INF
文件夹约定特定于 WAR 文件,而不是 JAR 文件。摆脱它。如果您确实需要一个单独的 JAR 文件夹作为类路径的一部分,请改用/META-INF
。Put it in root of the JAR and get it by context classloader instead of servletcontext.
The
/WEB-INF
folder convention is specific to WAR files, not to JAR files. Get rid of it. If you really need a separate JAR folder which is to be part of the classpath, use/META-INF
instead.您可以从给定实例访问标准类路径上的任何资源,
例如从“myservlet”类(错误命名)。
getServletContext().getResourceAsStream()
访问 Web 应用程序基目录上下文中的内容。在 jar 中包含 WEB-INF 目录似乎不太好 - 你会引起混乱。你就不能找到一个更好的名字吗?
You can access any resource on the standard classpath from a given instance
for example from your "myservlet" class (Bad naming).
getServletContext().getResourceAsStream()
accesses content in the context of the web application base directory.It seems bad style to incldue a WEB-INF directory in a jar - you would cause confusion. Can't you find a better name?
这是我发现的并且它对我有用。 @BalusC 提供的帮助对我有用。我整理了我所发现的内容以及我如何验证它是否有效。
我有一个具有以下结构的 Maven 项目,如下所示
现在,当我构建这个项目时; jar 看起来像
,这里 queries.properties 移动到“META-INF”下“ 文件夹。现在,如果这个 jar 有一个类试图使用这个属性文件
Thread.currentThread().getContextClassLoader().getResourceAsStream("queries.properties")
认为仍然可以在资源文件夹下访问同一个文件,如项目结构所示,这是错误的。正确的方法是通过 META-INF 文件夹访问
我如何验证
只需创建一个简单的 java 项目并将刚刚创建的 jar 包含到其构建路径中,然后创建一个具有 ClassLoader 的类的实例如上所述。这个新的 java 项目中的代码应该类似于
其中
Queries
是刚刚包含在构建路径中的 jar 中的类。This is what I have found and it worked for me. The help provided by @BalusC worked for me. I have collated what I have found and how I have verified that it is working.
I have got a maven project with following structure as shown below
Now when I build this project; the jar looks like
and here queries.properties moves under "META-INF" folder. Now if this jar has a class which is trying to utilize this property file using
Thread.currentThread().getContextClassLoader().getResourceAsStream("queries.properties")
thinking that the same file can still be accessed under resources folder as shown in project structure, that is wrong. The correct way is to access via META-INF folder
How did I verified
Just create a simple java project and include the jar you have just created into its build path and create an instance of class which is having ClassLoader statements as mentioned above. Your code in this new java project should look like
where
Queries
is a class in jar you have just included in your build path.