aspose file tools*
The moose likes Beginning Java and the fly likes File Not found exception.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "File Not found exception.." Watch "File Not found exception.." New topic
Author

File Not found exception..

A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

I am putting a properties file in the same package as my java class is.But when i am running the program,it cannot find it inside the folder where the class exists..

If i put the properties file outside the package,it is finding ....

Why so?

And what shall i do if i want the properties file to be inside the folder where the java file is ??


Here is the code..



If the properties file is in the parent folder of Javaranch,it works fine but i want the file to be inside the folder Utilities.

Regards
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14342
    
  22

If you use a FileInputStream with a filename, without specifying the directory where the file is in, as you are doing in your code, then Java will look in the current directory (i.e. the directory where you started the program from). It will not automatically search any subdirectories for your file. So if your file "parameters.properties" is not in the current directory, it will not find the file.

Specify the directory name if you want to open a file in a specific directory. For example:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi Jesper!!

Thanks for the reply...

But my doubt is...

When I run the program ...

will the program try to search for the file in the folder ...

where the actual java/class file exists or from the main folder

i.e. the folder where the package hierarchy starts...

folder where Javaranch (in the program) will be a subfolder

Regards
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14342
    
  22

It will only look for the file in the current directory, as I already wrote. It will not automatically search the classpath or any other kind of path.
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Thanks Jesper!!!
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Jesper Young:
It will only look for the file in the current directory, ...


Although there is still a trick here, since it possible to execute a Java app from any location, as long as the classpath is still valid. Therefore the definition of "current location" can change and cannot be assumed in the application.

It is often better to place the resource on the classpath (along with the application code) and use the ClassLoader to find the file.
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Sure David!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: File Not found exception..