how-to-load-resource-from-jar-file-packaged-in-a-war-file

http://stackoverflow.com/questions/4585553/how-to-load-resource-from-jar-file-packaged-in-a-war-file


Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share | improve this question
  add comment

3 Answers

up vote 21 down vote accepted

Put it in root of the JAR and get it by context classloader instead of servletcontext.

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("test.properties");
// ...

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.

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("META-INF/test.properties");
// ...
share | improve this answer
 
 
...or leave things as they are and use "WEB-INF/test.property". –   bkail  Jan 3 '11 at 15:33
 
@bkail, can also, but there's no reason to use WEB-INF folder since that isn't ever going to be accessible by servletcontext. It also doesn't make sense to put a WAR-specific folder convention in a plain vanilla JAR file. –   BalusC  Jan 3 '11 at 15:37 
 
Agreed, did not mean to imply your answer was incorrect. Just providing the OP another option in case changing the JAR structure is difficult or not possible for some reason. –   bkail  Jan 3 '11 at 15:39
 
thanks, it worked. –   Amit Patel  Jan 4 '11 at 4:36
 
Just to mention, "/META-INF/..." is incorrect, use "META-INF/..." instead. Anyway this answer helps us alot, thanks! –   cn1h  Mar 15 '12 at 9:57
show 1 more comment

You can access any resource on the standard classpath from a given instance

this.getClass().getResourceAsStream("name");

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?

share | improve this answer
  add comment

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

project structure

Now when I build this project; the jar looks like

enter image description here

and here queries.properties moves under "META-INF" folder. Now if this jar has a class which is trying to utilize this property file usingThread.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

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream is = classLoader.getResourceAsStream("META-INF/queries.properties");

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

public static void main(String[] args){
        new Queries();
    }

where Queries is a class in jar you have just included in your build path.

share | improve this answer
  add comment

Your Answer

 

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged   orask your own question.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值