This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Resources folder and creating an inputstream  RSS feed

 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The directory structure is a bit confusing to me. I thought I had this working but when I try to create an inputstream for a file in my resources folder I get a null pointer exception. Most likely I am not understanding how the directory structure should be set up. For management purposes I created a separate folder for sound files and other things that are not Java code. My directory structure looks like this which was mostly established by Eclipse:

I'm not sure the resources folder is at the correct directory level so I need some advice there. Also, I cannot seem to be able to access the files in that folder. When I use 'getResourceAsStream(path)' it returns a null pointer. I can't seem to figure out what to use for 'path'. TIA.
 
Saloon Keeper
Posts: 10249
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to add "resources" as a source folder of your project. I'm not sure how to do this in Eclipse, but you can have a look at your project properties. Most IDEs have a "Sources" tab or something like that.

Once you've set up your project so it uses both the "src" and the "resources" folders as source folders, you can load resources by giving an absolute path, or relative to the package the loading class is in.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. Does that mean that 'resources' should logically be at the same directory level as 'src'? I know it doesn't matter if the path is set correctly in sources but it seems to me to make more sense organizationally.

I've added the resources folder to my sources path. However, I still can't seem to get 'path' correct in my 'getResourceAsStream' call.
 
Stephan van Hulst
Saloon Keeper
Posts: 10249
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't matter, as long as it makes sense to you, and you're being consistent.

I tend to use Maven's standard directory layout, which looks like this:
 
Stephan van Hulst
Saloon Keeper
Posts: 10249
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dennis Putnam wrote:I've added the resources folder to my sources path. However, I still can't seem to get 'path' correct in my 'getResourceAsStream' call.



What is the path of your resource under the "resources" folder? What is the fully qualified name of the class you're using to load the resource? What's the path string you're using to load the resource?
 
Marshal
Posts: 5995
156
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You need to add "resources" as a source folder of your project. I'm not sure how to do this in Eclipse, ...


Select the resources folder in the Project Explorer and right-click. Go to Build Path -> Use as a Source Folder. Or look up Java Build Path in the project properties.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The file is directly in 'resources' as in my original post tree. I've tried a bunch of different things for the path but it always return a null. This is what I thought should work (this method is in the class named HTMLClass):

Among other things I also prefixed the file name with 'resources' and 'File.separator.resources'.
 
Stephan van Hulst
Saloon Keeper
Posts: 10249
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using File.separator? A resource name is not a file path.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously because I thought it WAS a file path. The examples I used certainly made it seem like one and at least one implied the file separator was required (keep in mind this has evolved trying different things I found). In any case removing that did not help.
 
Stephan van Hulst
Saloon Keeper
Posts: 10249
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like a file path, but it's not; it's a path to a resource. Look at the getResource() method documentation again to see what that means.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is where I started and it evolved to where I am now. Back to square one.
 
Stephan van Hulst
Saloon Keeper
Posts: 10249
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How did you get from that description to File.separator?
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to point to the correct location for the file I need to open. In any case this is irrelevant to my root problem of not being able to figure out how to specify the file (or getting the resources folder in the right place in my directory tree) I want to get as an inputstream.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finally found an example that works. I think I had this in one of my iterations but had something else wrong in the process:
 
Stephan van Hulst
Saloon Keeper
Posts: 10249
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're trying random things until something works. That's poor programming. Can you explain why this works, and the rest doesn't?

Dennis Putnam wrote:Trying to point to the correct location for the file I need to open. In any case this is irrelevant to my root problem of not being able to figure out how to specify the file (or getting the resources folder in the right place in my directory tree) I want to get as an inputstream.


Why do you figure it's irrelevant? It's not, I can assure you. Tell my how you went from the documentation of getResource() to File.separator. There's no mention of File.separator in that documentation.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've already explained how this entire problem evolved from that documentation to misleading examples.
 
Stephan van Hulst
Saloon Keeper
Posts: 10249
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, let's go for a concrete example.

Say you wanted to load a resource that's in resources/images/foo.png. What String would you pass to your method?
 
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!