Reputation: 834
I have a properties file in my Java program that stores several values. In the settings window, the user can edit these values and save the modifications for the next time he runs the program.
Here is the code that load properties form the properties file:
public class AppProperties {
private final static AppProperties appProperties = new AppProperties();
private static Properties properties;
private final static String preferencesSourcePath = "/res/pref/Properties.properties";
private AppProperties() {
properties = new Properties();
try {
properties.load(getClass().getResourceAsStream(preferencesSourcePath));
} catch (IOException e) {
e.printStackTrace();
}
}
And here, the method that save properties in the properties file (in the same class):
public static void saveAppPropertiesFile() {
try {
OutputStream outputStream = new FileOutputStream(new File(AppProperties.class.getResource(preferencesSourcePath).getPath()));
properties.store(outputStream, null);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I have tried this feature, it doing that job great when I am in my IDE but it doesn't work when I run the JAR file. In fact, it works for the current session but it's not saved in the JAR file.
In the console, it say:
java.io.FileNotFoundException: file:\C:\Users\HP\HEIG\EcoSimSOO\out\artifacts\EcoSimSOO_jar\EcoSimSOO.jar!\res\pref\Properties.properties (La syntaxe du nom de fichier, de répertoire ou de volume est incorrecte)
at ...
at res.pref.AppProperties.saveAppPropertiesFile(AppProperties.java:31)
It's where I try to do this:
AppProperties.class.getResource(preferencesSourcePath)
I have read this post but I don't understand the problem and what I have to do to fix this...
Thank you for your help.
Upvotes: 1
Views: 1073
Reputation: 140
You should not be writing anything to the JAR file. Technically, the resources under the JAR are read-only. You cannot write/modify any of the files inside the JAR.
I have a properties file in my Java program that stores several values. In the settings window, the user can edit these values and save the modifications for the next time he runs the program.
Instead of saving these modified values in the properties file, you can use a database/cache/flat-file to store these values and read from them during run-time.
Upvotes: 2