• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamically loading settings file  RSS feed

 
k mayank
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
I have a properties and a txt file which I need to load at run time. The problem is that I don't want to hard code the relative path ex ../sub/file

I tried the below command from static method of a class named CLassName

CLassName.class.getClassLoader().getResource(APP_CONFIG_FILE)

where APP_CONFIG_FILE is the file I want, however I am getting null back

Kindly suggest.

Regards,
Mayak
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use a ClassLoader object to load resources, those resources have to have absolute path. Use ClassName.class.getResource() instead.
 
k mayank
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

Thanks for your reply. But ClassName.class.getResource() only work in case resourse is in same folder as the ClassName. I wanted to search outside the jar in the class path folders. My file(fow which I need the path) is in /root/config/file.txt, while the classes are in /root/lib/proj.jar and I have to run this application from /root/scrdir/script.sh

Now is this possible at all to dynamically get path of file.txt

Regards,
Mayank
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the files are outside of the Jar, then why don't you use Java IO to read it as normal files. Have a look at java.io.FileReader. You can load properties with properties.load(InputStream), too. More info on in can be found in the IO tutorial
 
k mayank
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

Thanks for your reply.
I can read the file using JAVA IO. What I wanted to know was is it possible to check and load a file just by filename(in case i dont provide full path to the file) from a java program.

Regards
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think just a file name would be enough. Both Java IO and Class(Loader).getResource require a path - relative or absolute. If you don't want to hardcode the path you can put it in some other property file (but you'd need to get that file's path then) or require the user to submit it as a system property at startup (java -Dmypath=../some/path/to/file.txt) and get it using System.getProperty("mypath");
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your program will need to know where to look, so you will have to tell it where to look. The File class can help you out with multiple locations too:
 
k mayank
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob, Adam,

Thanks for your wonderful replies
Now I can see where I went wrong in my assumptions.
Thanks again for all the help.

Regards,
Mayank
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!