This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple java.io.FileNotFoundException problem?

 
James Hodgkiss
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
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java 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
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
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!)


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-

 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • 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: 3090
14
  • 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:
 
Jaikiran Pai
Marshal
Pie
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
Pie
Posts: 20665
65
Chrome Eclipse IDE Java 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