• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

FileNotFoundException When Trying to Read from a .properties file

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Phoenix,

Its better load the properties using the Classloader.

Check out this good article
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic