0

I am currently using the java class Preferences to maintain preferences inside of our java application. It is simple, and currently only contains 2 key/value pairs. Some of the higher ups on this project have requested to keep the preferences.xml file inside of the JAR, to make it a clean running, so everything would be self contained within the JAR (users do not need to be able to access this file).

I know that preferences has an import using an InputStream, and an export using an OutputStream. Is there anyway to edit the file inside the jar using the outputStream?

  • 2
    Not possible, but see https://stackoverflow.com/a/45998896/984823 – Joop Eggen Feb 05 '20 at 14:56
  • 1
    Does this answer your question? [Self exploding and rejaring jar file during execution](https://stackoverflow.com/questions/8731250/self-exploding-and-rejaring-jar-file-during-execution) (you will probably prefer the non accepted answer, and you should probably follow the accepted answer) – Aaron Feb 05 '20 at 15:04
  • 1
    And I would suggest having a default configuration file inside your jar which you would install for an user when there's not already one. Running the app with just the jar is still possible, it lets each user have their own preferences and that will still work if multiple users are using the same .jar on a multi-users machine – Aaron Feb 05 '20 at 15:09
  • The right way to do this—and the way all multi-platform applications are doing it—is to store the defaults in the .jar file, and use a known location in the file system, somewhere under the user’s home directory, to store any changes. If the file doesn’t exist, you should create it by copying the defaults that are stored in your .jar. (Note that the Preferences class does not use a preferences.xml file in Windows, unless you [export](https://docs.oracle.com/en/java/javase/13/docs/api/java.prefs/java/util/prefs/Preferences.html#exportSubtree%28java.io.OutputStream%29) the preferences.) – VGR Feb 05 '20 at 16:59

2 Answers2

2

A JAR file is a zip folder, so it can be extracted and worked upon using the java.util.zip package.

However, a running jar may be write protected, preventing content modification.

It is better suited to store configuration in a corresponding location in the operating system. On windows, the common location is the windows registry.

There is a Java library for editing the registry as shown in this answer .

Library Source: https://github.com/apache/npanday/tree/trunk/components/dotnet-registry/src/main/java/npanday/registry

To store config on other operating systems, you can use a file inside a folder named after your application.

  • Common Location on Linux: '~/.config' folder
  • Common location on Mac: '~/Library/Application Support' folder.
0

I don't think it's possible to edit a file inside a jar using OutputStream.

What you can do is this: You can import the file, edit it and add it back to the jar file. For pushing a file into jar, refer adding a file to jar

Lokesh P
  • 91
  • 1
  • 5