javaapache-commons-config

Apache Commons Configuration Cant save


I am using Apache Commons Configuration to keep some properties in a properties file located in a package re/iprocu/coperativeerp/config/payment/configurations.properties

private PropertiesConfiguration configs = new  PropertiesConfiguration("re/iprocu/coperativeerp/config/payment/configurations.properties");
configs.setAutoSave(true);
configs.setProperty(date.getYear()+"-"+date.getMonthValue()+"-01", offsetMember);

The problem is when i run the above code i get the following error

Exception in thread "JavaFX Application Thread" org.apache.commons.configuration.ConfigurationRuntimeException: Failed to auto-save
at org.apache.commons.configuration.AbstractFileConfiguration.possiblySave(AbstractFileConfiguration.java:753)
at org.apache.commons.configuration.AbstractFileConfiguration.clearProperty(AbstractFileConfiguration.java:799)
at org.apache.commons.configuration.AbstractConfiguration.setProperty(AbstractConfiguration.java:485)
at org.apache.commons.configuration.AbstractFileConfiguration.setProperty(AbstractFileConfiguration.java:788)

Update

Apache Commons Configuration V. 1.10

Maven dependency

<dependency>
     <groupId>commons-configuration</groupId>
     <artifactId>commons-configuration</artifactId>
     <version>1.10</version>
</dependency>

Solution

  • You cannot save a configuration that points to a file within a JAR. Try using a configuration file that is located outside of your JAR and you'll see that auto-save option works correctly.

    EDIT

    You can put the file both in a JAR and in the same relative path in one of the default locations. Here's what the documentation says.

    If you do not specify an absolute path, the file will be searched automatically in the following locations:

    • in the current directory
    • in the user home directory
    • in the classpath

    So, if for instance you have a default properties file in you JAR, located at conf/configuration.properties, you can put the properties either in user's home directory under the same relative path - e.g. on Linux /home/username/conf/configuration.properties or in the same directory with your JAR file. Both should be secure enough.