Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Simple java.io.FileNotFoundException problem?  RSS feed

 
Ranch Hand
Posts: 401
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating a desktop Java application. It consists of one class and one properties file (both in the same package).

Why does the following line give a FileNotFoundException? How can I solve?...

Properties props = new Properties();
props.load(new FileInputStream("config.properties"));

Results in:
java.io.FileNotFoundException: config.properties (The system cannot find the file specified)

Any help much appreciated!

Thanks,
James
 
Sheriff
Posts: 21502
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because the current directory is the base of your package tree, not the directory local to the class file. you should switch to using resources instead; check Class.getResourceAsStream:
(And you should close() all your streams when done!)
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice one, thanks!
 
Rob Spoor
Sheriff
Posts: 21502
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:That's because the current directory is the base of your package tree, not the directory local to the class file. You should switch to using resources instead; check Class.getResourceAsStream:
(And you should close() all your streams when done!)



I am making code in eclipse and getting the same error. What is meant by current directory and ?
I was trying to split your command and I get errors. How do I fix those ?



error -
java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.ClassLoader

Why is the the fileinputstream (fis) not able to work in these situations ? How does the ClassLoader approach work ?
What happens under the hood ?
 
David Jason
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:That's because the current directory is the base of your package tree, not the directory local to the class file. You should switch to using resources instead; check Class.getResourceAsStream:
(And you should close() all your streams when done!)



Did not work for me.
Got this error-

 
Ranch Hand
Posts: 3109
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably need to make sure that your properties file is copied to the same directory as the .class files, not the .java files.
 
Mike Simmons
Ranch Hand
Posts: 3109
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The getClass() method returns a Class, not a ClassLoader. You should look at the documentation for java.lang.Class to see what methods it has. If you want to use a FileInputStream, that should be possible too:
 
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote: If you want to use a FileInputStream, that should be possible too:



That I believe will fail if that config.properties ultimately ends up being within a jar distribution of that application. I admit the user here is talking about running it in Eclipse, but I think ultimately the application will end up being packaged.

 
David Jason
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jaikiran Pai wrote:

Mike Simmons wrote: If you want to use a FileInputStream, that should be possible too:



That I believe will fail if that config.properties ultimately ends up being within a jar distribution of that application. I admit the user here is talking about running it in Eclipse, but I think ultimately the application will end up being packaged.



Yes, i might have to do that. Good point.
 
Rob Spoor
Sheriff
Posts: 21502
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jaikiran Pai wrote:That I believe will fail if that config.properties ultimately ends up being within a jar distribution of that application.


It definitely will. That's why you should use getResourceAsStream when you need an InputStream. That will also return null if the resource cannot be found. It (usually) actually uses getResource in the background. From the current source code:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!