Question

I have a standart maven project:

src/

| --- main/*.java

| --- resources/

    |--- settings1.ini

    |--- settings2.ini

That's the way I am reading this settings files:

InputStream settingsFileInputStream = ClassLoader.getSystemResourceAsStream(this.configurationFileName);

All works fine but I have to provide functionality to reload these settings1.ini and settings2.ini files at runtime when I am running maven-compiled .jar file. How can I do this?

Because as far as I know I can't access any data in .jar archive and modify it.

Was it helpful?

Solution

You can access a file in one of your application's JAR files (assuming it is on the classpath) using a stream opened using ClassLoader.getResourceAsStream(resourcePath).

However, you cannot update a file in a JAR file. Or to be more accurate:

  • updating a JAR (using the Java SE libraries) entails rewriting it,

  • there are many situations where a application won't be able to write to its JAR file,

  • even if it can do it, the application may only see the results of the updates after it has been restarted, and

  • it is a bad idea for an application to update itself in this way for various reasons ... including security.


If you want the file to be updateable, I suggest the following approach:

  1. Pick a standard location for the file on the user's machine; e.g. on Linux, you might pick a hidden subdirectory of the current user's home directory.

  2. On starting the application, see if the file exists, and if it doesn't populate it from the copy in the JAR file.

  3. When the application then needs to read or update the file, read or update it at the above location.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top