Alex Rivera | Logout

Accessing properties files outside the .jar?

Asked 2009-04-22T01:32:21.413
14

I have a .jar file I'm putting together. I want to create a really really simple .properties file with configurable things like the user's name & other stuff, so that they can hand-edit rather than my having to include a GUI editor.

What I'd like to do is to be able to search, in this order:

  1. a specified properties file (args[0])
  2. MyApp.properties in the current directory (the directory from which Java was called)
  3. MyApp.properties in the user's directory (the user.home system property?)
  4. MyApp.properties in the directory where the application .jar is stored

I know how to access #1 and #3 (I think), but how can I determine at runtime #2 and #4?

Edit
Report

1 Answer

6

#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... */
}
answered 2009-04-22T03:07:37.957

Your Answer