Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Accessing properties file in a jar file from code in the same jar file

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am sort of stuck with an implementation that I have been asigned.It is a standalone runnable jar that I have to create from an eclipse project. This eclipse project runs fine when run from Eclipse IDE. But when I export it as a runnable jar and then when I run the jar using :

File f =new File("abc.properties");

the code searches in the folder in which I have placed my runnable jar and NOT WITHIN the jar itself.I want to ideally put evrythin in the jar file and then run only the jar file from command prompt.Because of this issue I have to keep the properties file with the jar file in the same folder so that my File IO picks it up.


Can anyone please suggest me a solution to put it in the jar and still use it.



 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the getResource() or getResourceAsStream() APIs on the ClassLoader class to fetch resource. All you have to do is use the correct classloader and based on what you say, I think using the classloader of the class (@see Class.getClassLoader() API) from where you are looking up this file should work.
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put the file on the classpath and load the properties using
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Slightly better, because it closes the InputStream:
 
Amol Kshirsagar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

Really appreciate your quick response. I got the solution.I used

Properties properties = new Properties();
properties.load($MyClassName$.class.getResourceAsStream("/Environment.properties"));

Here '/' was the main catch since the properties file was placed immediately in the jar file and not under any folder in the jar.

Once again thanks for your help !!!


 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Slightly better, because it closes the InputStream:



Much better actually.
Amol you should check the API specs for that Properties.load method which say

The specified stream remains open after this method returns.

and therefore use the try with resources if on Java 7 or close the stream in a finally if on an earlier Java version.
 
please buy my thing and then I'll have more money:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic