Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading and writing to a properties file that is outside of the jar file  RSS feed

 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

is it possible to read/write a properties file that is outside of the application jar file? without using an absolute path, I need to run a configuration method only if the properties file does not contain any properties/values. originally I was using InputStream and getResourceAsStream(path); which worked fine when running the main method in the ide but fails once I have created the jar file.

Martyn
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martyn Clark wrote:is it possible to read/write a properties file that is outside of the application jar file?

Of course. It's when the properties file is inside a jar file that you can't write it.
without using an absolute path

You don't need to use an absolute path if you don't mind your relative path being relative to the current working directory which the user set (or accepted the default for). Generally it's better to choose a reasonable place to store the properties file rather than relying on unpredictable circumstances like that.
I need to run a configuration method only if the properties file does not contain any properties/values. originally I was using InputStream and getResourceAsStream(path); which worked fine when running the main method in the ide but fails once I have created the jar file.

I'm not sure what that means. If you're going to have a default version of the properties in the jar, but the user can change that and store their personal version somewhere else, then you're going to have to check the "somewhere else" first, and if there's nothing there then use the default version.

As for the remark about the IDE, that suggests you're being held hostage by your IDE. If you weren't aware of the concept of "current working directory" before, then study up on that and how it works. Then find out what your IDE uses for its current working directory. You may not be able to change that, or even want to change it, but you should at least know what it is so you know what to expect.

And if I'm right about your desires, a good place for the "somewhere else" is the user's home directory. There's a Java system value which points to that but I forget exactly what it is.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!