• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing resource in jar via ServletContext.getResourceAsStream()

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

How can i access resource (e.g. txt file) through this method ?
I've tried url like "/META-INF/myjar.jar/MANIFEST.MF"

what url shuld i use do get this resource?
 
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you need to look on the classpath. Assuming your Jar is on the classpath, and the file is in the com/mypackage directory of the jar, you can do something like:Note that it's been a while since I've done this and my comments are from memory... I just wanted to point out that you're looking on the classpath, not in the file system.
 
Sergio Puchini
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think ServletContext.getResourceAsStream() is more safe to use, since its servlet container api. And question was about this method.
Beside that, if i have 2 jar files in my lib wich have same files, and i must get them, what should i do... in this case a cant use class loaders because this files may not have any classes at all.
In scwcd book i've read that this method supports jar files, but i can't find any information how to work with jars this fashion.
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I know, getResourceAsStream is pulling stuff via the classpath. It doesn't matter if what you're looking for is a class file or not. For example, if you have a Jar file on the classpath of the application, and it contains a file named myfile.txt in the root of the Jar, you get get it using getResourceAsStream("myfile.txt").

If you have two Jar files on the classpath and they both contain the same file, it will get which ever one is first on the classpath. Basically, don't do this.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark E Hansen wrote:As far as I know, getResourceAsStream is pulling stuff via the classpath...


That's only true of the class loader's version. The method on ServletContext searches the hierarchy under the context root.
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, Thanks Bear!
 
Sergio Puchini
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

Mark E Hansen wrote:As far as I know, getResourceAsStream is pulling stuff via the classpath...


That's only true of the class loader's version. The method on ServletContext searches the hierarchy under the context root.



So, question is : can i get file from jar archive wich is in my context root (or lib folder, doesnt matter) whith this method, something like this


i've tryed... it doesnt work...
 
Ranch Hand
Posts: 56
Eclipse IDE Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sergio Puchini wrote:
So, question is : can i get file from jar archive wich is in my context root (or lib folder, doesnt matter) whith this method, something like this


i've tryed... it doesnt work...




The path must begin with a "/" and is interpreted as relative to the current context root. Please review the servlet API.
 
Sergio Puchini
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


doesnt work too

nor

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The jar will be loaded onto the classpath. So why aren't you using the classloader to try and fetch a resource in it?
 
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



The jar will be loaded onto the classpath. So why aren't you using the classloader to try and fetch a resource in it?



Class.getResourceAsStream() will load only classes and not text files.Correct me if i am wrong.


Thanks,
Vipul.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are wrong.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic