Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

how to make a jar use an external properties file (in same directory)?

 
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am currently running a jar file and want it to pick up a config file in the same location where the jar lives (jar to use external config file).

Here is my code:



The problem is this does not pick up the external config file.

Is there a way to do this?

Thanks in advance

 
Saloon Keeper
Posts: 14327
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the jar doesn't represent your main executable, then I would just pass the relevant methods a Properties object directly. Make the main executable responsible for loading the file.

getResourceAsStream() requires the resource to be on the class path. You can make a separate source folder for your config files, and put it on the class path. If you don't want to do that, you should use a FileInputStream instead.
 
Kyle Jones
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.

Was actually easier than I thought.. Got there using the FileInputStream

Here is the change:


Cheers!
 
Marshal
Posts: 27379
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Easier, yes, but you changed the requirements. Instead of looking in the same directory as the jar file, you are looking in the current working directory. If that works for you then that's good.
 
Kyle Jones
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are right, but (something i didn't mention)

I am using the maven assembly plugin to create a distribution where the jar and config file are both inside a tar.gz file.
 
Stephan van Hulst
Saloon Keeper
Posts: 14327
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally I think the best way to do this is to use the class path correctly. Add the config directory to your project, have maven copy it to the output directory, and add the config directory to the class path in your jar's manifest.

You can now use the getResourceAsStream method to load the properties file, which I think is more robust than using files.
 
You save more money with a clothesline than dozens of light bulb purchases. Tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic