• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File Not found exception..

 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper!!!
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure David!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic