aspose file tools*
The moose likes Servlets and the fly likes Accessing resource in jar via ServletContext.getResourceAsStream() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Accessing resource in jar via ServletContext.getResourceAsStream()" Watch "Accessing resource in jar via ServletContext.getResourceAsStream()" New topic
Author

Accessing resource in jar via ServletContext.getResourceAsStream()

Sergio Puchini
Greenhorn

Joined: Dec 03, 2009
Posts: 11
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?
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
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

Joined: Dec 03, 2009
Posts: 11
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

Joined: Apr 01, 2009
Posts: 643
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
Ah, Thanks Bear!
Sergio Puchini
Greenhorn

Joined: Dec 03, 2009
Posts: 11
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...
Charles 'King
Ranch Hand

Joined: Jul 05, 2009
Posts: 56

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

Joined: Dec 03, 2009
Posts: 11


doesnt work too

nor

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

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

Joined: Oct 14, 2010
Posts: 218


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.


Thanks
Vipul Kumar
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

You are wrong.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing resource in jar via ServletContext.getResourceAsStream()