This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Simple java.io.FileNotFoundException problem? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Simple java.io.FileNotFoundException problem?" Watch "Simple java.io.FileNotFoundException problem?" New topic
Author

Simple java.io.FileNotFoundException problem?

James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
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

Joined: Oct 27, 2005
Posts: 19556
    
  16

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!)


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
Nice one, thanks!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

You're welcome.
David Jason
Greenhorn

Joined: Aug 21, 2012
Posts: 27
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

Joined: Aug 21, 2012
Posts: 27
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

Joined: Mar 05, 2008
Posts: 2970
    
    9
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

Joined: Mar 05, 2008
Posts: 2970
    
    9
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

Joined: Jul 20, 2005
Posts: 9343
    
111

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.


[My Blog] [JavaRanch Journal]
David Jason
Greenhorn

Joined: Aug 21, 2012
Posts: 27
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

Joined: Oct 27, 2005
Posts: 19556
    
  16

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:
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Simple java.io.FileNotFoundException problem?
 
Similar Threads
Accessing files that are there in classes directory
Exception while accessing .properties file.
Jar a project with property files in it external to jar
Resource Bundle Error
How to read properties file in netbeans?