File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Loading external properties file relative to the Runnable Jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Loading external properties file relative to the Runnable Jar" Watch "Loading external properties file relative to the Runnable Jar" New topic
Author

Loading external properties file relative to the Runnable Jar

S Chan
Ranch Hand

Joined: Jul 25, 2011
Posts: 51
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.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
S Chan
Ranch Hand

Joined: Jul 25, 2011
Posts: 51
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?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8158
    
  23

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

Joined: Jul 25, 2011
Posts: 51
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.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 871
    
    5
> 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

Joined: Jul 25, 2011
Posts: 51
Ivan Jozsef Balazs, thank you. That rang my bell!
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 871
    
    5
Welcome!

It is relatively new and I know about it, although I have never used it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loading external properties file relative to the Runnable Jar