• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Loading external properties file relative to the Runnable Jar

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

I am a newbie and currently involved in this project which requires me to load an external properties file. I have Googled a bit and saw quite a few different ways of achieving this, but none has satisfied what I want.

My project would ultimately deploy as a Runnable Jar, so I need a way to load an external properties file with a path relative to the Runnable Jar's location. Also, another problem is that, at development stage, I have to put the properties file inside the src folder of the Eclipse workspace, otherwise, Eclipse won't be able to locate the properties file.

How should I put/set up the properties file so that:
1. When I go File>Export>Runnable Jar file on my eclipse, the exported Jar will not include the properties file inside;
2. The properties file is readable/accessible while I am developing/debugging in eclipse environment
3. The properties file is accessible with relative path while the exported Jar is still running
4. There is no need to change in code and hard-coded path between development in exporting.

Last but not least, since I am just a newbie, I would really appreciate if you can provide me a few lines of demo code meeting my requirements above.

Thank you.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If a property file is required by a jar, it is a general practice that the property file is packaged inside the jar. I'm not sure why you are keeping it out of jar.

Anyways, what you want can be achieved by one of the following ways:

1) Keep your property file in a directory (say /config/props) and keep that dir in CLASSPATH (or provide it as -cp option during runtime).
2) Or, assign an env variable to the path of that dir, and then access the file (e.g. $CONFIG_PATH=/config/props, and then access $CONFIG_PATH/config.properties)
3) Keep the property file at a specific relative location from current jar, and then access it (e.g. ../config/config.properties).

Even if you are using IDE like Eclipse, it is not necessary to keep your property file under src dir. You can still keep the file at location of your choice, and include it in classpath (or env variable) while executing your code in Eclipse.

However, please be sure to handle FileNotFoundException in all cases.

Personally, I'll strongly suggest to keep file inside jar, or follow first option from above list.

I hope this helps.
 
S Chan
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Anayonkar for the fast reply.

The reason that I want to keep the properties file outside the Jar, is that after deployment, I would still like to use a text editor to edit/view the properties file in order to change certain behaviour of the program. Does that make good sense? Or is that not wise to do so?

I thought about using adding classpath or env variable, but a problem is that if I distribute the jar(along with the properties file) to other people. Those people would have to need to add classpath or env variable on their machines before the program could function properly. Am I right at this point? I would prefer not to specify any classpath or env variable so that the users would not need to do that and run the Jar on different machines portably and without certain admin/system permissions.

To be frank, I wish to distribute the whole thing in a ZIP file or any other archive file formats. Inside the ZIP file, there contains a Jar and some directories containing properties files and some resource files. When the users receive the ZIP file, they can extract everything into one directory and run the Jar directly.

If i were to use the third method, using the relative location from current Jar. How should I set that up for an IDE like Eclipse? what is that "relative location" for the IDE workspace?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Chan wrote:The reason that I want to keep the properties file outside the Jar, is that after deployment, I would still like to use a text editor to edit/view the properties file in order to change certain behaviour of the program. Does that make good sense? Or is that not wise to do so?


Probably not. The words "properties" and "dynamic" don't generally go well together, since properties are generally used for configuration data.

Now if this is for testing, that's completely different. Eclipse files are just like any other, so if you want simply want to test your program with various configs, just edit the file where you put it (ie, as Anayonkar suggested) and run it inside Eclipse.

The fact is that a jar is an archive, so everything in it will be a copy anyway. When you run a program inside an IDE like Eclipse, it actually runs it 'on the fly'.

If you really want your users to be mucking about with properties, I suspect there's a better way of doing it; but without more information, it's difficult to advise.

Why not give us a bigger picture of what you want to achieve, rather than how you want to do it?

Winston
 
S Chan
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

S Chan wrote:The reason that I want to keep the properties file outside the Jar, is that after deployment, I would still like to use a text editor to edit/view the properties file in order to change certain behaviour of the program. Does that make good sense? Or is that not wise to do so?




Why not give us a bigger picture of what you want to achieve, rather than how you want to do it?

Winston



True. I get what you mean. Thank you for the reply.

I am using the properties files to store a lot of configuration values for the program.

For instance in one part of the program, I need to find a piece of thrid party/external software in the running machine to see if that software exists. If so, launch it when user wants so. I want to include the default location of the launcher/executable of the software. So I just thought, since I am having firiends who have different OSs, from WIndows, Mac to different GNU Linux distro, it would be good if the users can modify the properties for the first time and forget about the trouble the next time they run the program. Since most of them have Windows machines, I think it would be good to include this in the properties file:
launcher.location=C:\\Program Files\\The Software\\run.exe
and let them modify the value manually if they need to.

Also, another thing is that, I want to have my program to store/save certain settings as the users change these settings through GUI. I think the easiest way to read/write these changed settings is using properties files. Am I right? Although the setting-saving frequency may not be high, the setting-reading is vital everytime the Jar runs. I don't think it would be wise to modify/write the properties file if it is inside the Jar. I don't want to do anything about writing files inside the packaged Jar when the Jar is running. It would be messy and I doubt if that is even possible.

Let me know if there are better solutions than this. I would appreciate it very much.
 
Rancher
Posts: 1044
6
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> I think the easiest way to read/write these changed settings is using properties files. Am I right?

Java Preferences API
 
S Chan
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs, thank you. That rang my bell!
 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome!

It is relatively new and I know about it, although I have never used it.
 
Pay attention! Tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic