Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing resource in jar via ServletContext.getResourceAsStream()

 
Sergio Puchini
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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?
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Ah, Thanks Bear!
 
Sergio Puchini
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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...
 
Charles 'King
Ranch Hand
Posts: 56
Eclipse IDE Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator


doesnt work too

nor

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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?
 
vipul bondugula
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are wrong.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic