jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Accessig the properties file from outside the jar file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Accessig the properties file from outside the jar file" Watch "Accessig the properties file from outside the jar file" New topic

Accessig the properties file from outside the jar file

Pankaj Shet
Ranch Hand

Joined: Sep 08, 2006
Posts: 289

Hi Friends,
I am using Netbeans IDE for application development.
I am having a properties file for configuration which I am putting in the jar and accessing it via resourcebundle.
But what i want is put the properties file outside the jar and access it in the java class inside jar.
The jar file which i am using is the one from the dist folder of the project.
please help me with example how should i access the same?

Help is always appreciated.

Thanks and regards,

B.Sc.(I.T.), S.C.J.P., S.C.W.C.D., PGDAC(CDAC)
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

If you are program is standalone Java application, you can pass the absolute path of "Properties" file via command line of the program...

SCJP 6 [SCJP - Old is Gold]
Pankaj Shet
Ranch Hand

Joined: Sep 08, 2006
Posts: 289

Thanks for reply.
Yes my program is a stand alone application.
Giving path via command line argument will ask for the properties file path again and again as many times you run the application.
I want to put the properties along with jar file in same folder that is, I want abc.properties file and xyz.jar file in the same folder lmn.
xyz.jar will contain only Main.class file and nothing else other than Main.class and the files created by jar utility.
abc.properties file will be outside the jar file inside the same folder in which jar file is kept.

When abc.properties was inside the xyz.jar, Main class accessed it via ResourceBundle.getBundle("abc",Locale.getDefault())
How should I access the abc.properties file from outside the jar file using the Main.class ?

Please provide code snippet for the same.

Help is always appreciated.

Thanks and Regards,
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Well, no. Here's what you should do:

First, put your configuration file into the jar file and access it there -- you know how to do that.

Next, if you want to allow the user to modify or personalize the configuration, then store a copy of the configuration file in the user's home directory after the user makes changes to it. You can get the name of the user's home directory from the "user.home" system property.

Then to find the configuration for the user, first look in the user's home directory. If you find the file there, that's what you should use. If you don't, then you should use the default version which you deployed in the jar file.

(By the way: this doesn't seem to have anything to do with those certification exams. Let's move the thread to a regular Java forum.)
jimmy Auction

Joined: Jan 26, 2012
Posts: 5
1. .
2. is the "user.dir" system property.
3. is the "user.home" property.

is a bit of a kludge no matter how you approach it. Here's an alternate technique that works if you have a class loaded from a JAR not on the system classpath.

CodeSource src = MyClass.class.getProtectionDomain().getCodeSource();
if (src != null) {
URL url = new URL(src.getLocation(), "MyApp.properties");
else {
/* Fail... */

I agree. Here's the link: http://aspose.com/file-tools
subject: Accessig the properties file from outside the jar file
It's not a secret anymore!