• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessig the properties file from outside the jar file

 
Pankaj Shet
Ranch Hand
Posts: 293
Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
-Pankaj
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are program is standalone Java application, you can pass the absolute path of "Properties" file via command line of the program...

 
Pankaj Shet
Ranch Hand
Posts: 293
Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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,
-Pankaj.
 
Paul Clapham
Sheriff
Pie
Posts: 20735
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. .
2. is the "user.dir" system property.
3. is the "user.home" property.
4.

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... */
}

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic