There is a class that is a wrapper for the Properties class that was conceived for accessing the values ​​of the config.properties which is inside the jar of the project itself:

 public class Prop { public static final String EMAIL = "mail"; public static final String PASS = "password"; public static final String RECIPENTS = "recipents"; public static final String FIO = "fio"; public static final String USER = "user"; public static final String EMAIL_JIRA = "mailJira"; public static final String PASS_JIRA = "passwordJira"; private Properties properties; private Reader reader; public Prop() { properties = new Properties(); try { reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("config.properties"), "utf-8")); } catch (Exception e) { e.printStackTrace(); } try { properties.load(reader); } catch (Exception e) { e.printStackTrace(); } } public String get(String key) { return properties.getProperty(key); } public void set(String key, String value) { properties.setProperty(key, value); } } 

How can I change the data inside the config.properties using the setProperty() method? As far as I understand, you need to work with the object of the Properties class, which before editing we store() (load). But how then to get or create an OutputStreamWriter by analogy with InputStreamReader , and the getResourcesAsStream() method

  • Inside the jar - in any way (if you do not want to bother with repacking the jar and solve possible problems blocking this jar on the record). - Roman
  • @Roman you suggest to use (for example) ZipFileReader to read / write from the archive (after all, is the jar archive?). Simply, the essence of the task is to parameterize the values ​​that the program works with, but you don’t want to take out the file outside the jar and allow the user to accidentally break the keys or even delete the config.properties file - abbath0767
  • I suggest storing the file outside the jar. Many programs store the configuration in separate files and are not afraid if the user deletes them (he is to blame). - Roman
  • @ abbath0767 then create jpa and write \ read there. and the user will not be able to break anything - Senior Pomidor
  • @Roman in your words is true) probably will do so - abbath0767

1 answer 1

I can offer this option (for linux, under windows adapt by analogy) :

 List<String> lines = Arrays.asList("hello", "world"); Files.write(Paths.get("file.txt"), lines, StandardCharsets.UTF_8); Process process = new ProcessBuilder().command("/bin/bash", "-c", "jar uf solution.jar file.txt").start(); System.out.println(process.waitFor()); 

I'll sign for more in detail what is happening here. We create the file file.txt , then call the console command jar uf <название jar архива>.jar <название файлов которые хотим добавить> . We start the process, wait for its completion and display the return code. If it is! = 0, then trouble has happened and an error has occurred somewhere.

  • int exitCode = process.waitFor(); better than empty loop. - Roman
  • interesting solution. You can easily replace the txt file with properties and do without the list of strings. Maybe. But I’m scared by the prospect of the flow of the jar file being executed to try to change myself - this cannot lead to any problems? Nevertheless, I will try this solution - abbath0767
  • @Roman thanks, good point - Artem Konovalov
  • one
    @ abbath0767 generally storing mutable files in a jar archive is not a good practice. they should be read-only. - Artem Konovalov
  • How would you advise to implement the following problem: there is a project that is responsible for collecting data and sending a certain report, but the project must support the ability to change the parameters with which it works, for example, after receiving a jar, one user uses his or her key ring of passwords, and the other is different. The only option in my opinion is to use the properties file outside the project, but then the user gets access to the file and its data (including keys) that it is desirable not to touch. - abbath0767