This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Dynamically loading settings file  RSS feed

 
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
 
Sheriff
Posts: 21638
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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
 
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: 21638
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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
 
Don't listen to Steve. Just read this tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!