Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes FileNotFound exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "FileNotFound exception" Watch "FileNotFound exception" New topic
Author

FileNotFound exception

sam liya
Ranch Hand

Joined: Nov 25, 2008
Posts: 1204
this is my class




my properties file also in package called my.
but it gives this error

Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Aruna,
Its should be absolute path of the properties file.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34


Aruna,
Its should be absolute path of the properties file.


True, but not very practical, is it?

The path used by the FileInputStream constructor is relative to the current working directory of the JVM; it has nothing to do with the package structure. If you want paths relative to your class files, use Class.getResource() to open the file instead.


[Jess in Action][AskingGoodQuestions]
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



True, but not very practical, is it?


Yup ,especially when the application is to shipped to different machine. Class.getResourceAsStream or ClassLoader.getResourceAsStream is good candidate then.
sam liya
Ranch Hand

Joined: Nov 25, 2008
Posts: 1204
can anyone tell me how to use getResource() in this case
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



Read this
sam liya
Ranch Hand

Joined: Nov 25, 2008
Posts: 1204
I change my code to this .but still error



Error

Manhar Puri
Ranch Hand

Joined: Aug 23, 2005
Posts: 41
I am not sure why you commented out the line
FileInputStream url= this.getClass().getResourceAsStream("properties");


Is there a reason for changing the path from "properties" to "../properties"?

Try the code below:


Also please provide the name of your properties file as well location.

Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



FileInputStream url= this.getClass().getResourceAsStream("properties");


ClassLoader does not add any extension !! Provide complete file name with extn if there is any.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FileNotFound exception