aspose file tools*
The moose likes Beginning Java and the fly likes FileNotFoundException When Trying to Read from a .properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "FileNotFoundException When Trying to Read from a .properties file" Watch "FileNotFoundException When Trying to Read from a .properties file" New topic
Author

FileNotFoundException When Trying to Read from a .properties file

Phoenix Kilimba
Ranch Hand

Joined: Oct 10, 2006
Posts: 64
Dear Sirs and Madames,
I am trying to read from a properties file message.properties which is in the SAME package as the calling class. However I keep getting the rintime error message java.io.FileNotFoundException: message.properties (The system cannot find the file specified). I have racked my head as to why this could be but to no avail, for the dake of my sanity could someone please help?
Sample code(message.properties):


Sample code(PropertiesTest.java):



Runtime output:



As I said , they are both in the same package so cannot for the life of me think why the error...

Thanking you profusely in advance,

You are what you know
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Phoenix,

Its better load the properties using the Classloader.

Check out this good article
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
If you compile your java file, it will go to <targetdirectory>/openhrs/PropertiesTest.class. Your properties file should be in <targetdirectory>.

Alternatively, you can use PropertyResourceBundle and just pass the constructor a reader for your file (so you can have the property file in an arbitrary location).

Hope this helps!
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10266
    
    8

Class loaders are helpful only for resources packaged inside jar files. The are not going to help you out if you provide the wrong file path.

message.properties which is in the SAME package as the calling class

Are your .java and .class files in the same folder?
Is your .properties file in the folder containing the .class files?

What do you get when you do System.out.println(System.getProperty("user.dir")); Is your properties file at that location?

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

Class loaders are helpful only for resources packaged inside jar files. The are not going to help you out if you provide the wrong file path.


Not really, it is useful for applications where you do not want to hard code file paths in your code (as long as its in CLASSPATH)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Balu Sadhasivam wrote:
Class loaders are helpful only for resources packaged inside jar files. The are not going to help you out if you provide the wrong file path.


Not really, it is useful for applications where you do not want to hard code file paths in your code (as long as its in CLASSPATH)



Resources are usually located relative to the class(es) that need them; quite often in the same folder as the class file. Whether that folder is located in a JAR file or not doesn't matter.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10266
    
    8

Balu Sadhasivam wrote:
Class loaders are helpful only for resources packaged inside jar files. The are not going to help you out if you provide the wrong file path.

Not really, it is useful for applications where you do not want to hard code file paths in your code (as long as its in CLASSPATH)


So how does the class loader help you if you provide the wrong file path?
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



So how does the class loader help you if you provide the wrong file path?


With class loader , you really don't need the file path at all. Place the property file under any directory which is in CLASSPATH and load it. if its inside package , of course you got to give "package path" and not "file path"
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10266
    
    8

Balu Sadhasivam wrote:

So how does the class loader help you if you provide the wrong file path?


With class loader , you really don't need the file path at all. Place the property file under any directory which is in CLASSPATH and load it. if its inside package , of course you got to give "package path" and not "file path"


Package path is nothing but relative file path. If you get that wrong, you will still end up with an exception won't you?
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



Package path is nothing but relative file path. If you get that wrong, you will still end up with an exception won't you?


Yeah , that's obvious.
I meant , loading resources with Class Loader's inputstream has advantages rather than depending on FileInputStream. Do you disagree ?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10266
    
    8

Balu Sadhasivam wrote:
Package path is nothing but relative file path. If you get that wrong, you will still end up with an exception won't you?

Yeah , that's obvious.
I meant , loading resources with Class Loader's inputstream has advantages rather than depending on FileInputStream. Do you disagree ?


Nope. I guess we were talking about two related but different things.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Using class loaders and resources will of course still require the resources to be put in the right locations, but the "magic" is that that location is always relative to the class path, not the current directory. If you move the JAR file, or the folder with the classes and resources, or simply execute the program from a different directory, the resource will still be found. That won't necessarily be the case if you use File and FileInputStream.
 
 
subject: FileNotFoundException When Trying to Read from a .properties file