9 Replies Latest reply: Jan 16, 2009 10:23 AM by 807589 RSS

    can't update files inside a jar

    807589
      Hi all,
      Is it possible to update my xml files inside a jar?
      I dont think so?

      Please confirm.
        • 1. Re: can't update files inside a jar
          Alan.M
          Hi,
          package java.util.jar
          You can use Java's JarFile and JarEntry classes to read the contents of a JAR file, then use JarOutputStream to create a new one.

          So the process is to read the content first, filter out things which need modification ( the xml file), then build your entry based on what you need ( the modified xml file and the other entries i.e classes, etc..)


          Regards,
          Alan Mehio
          London,UK
          • 2. Re: can't update files inside a jar
            800431
            Sure, you can!
            A jar file is Java version of zip files,
            You would first extract the contents of the jar file, make updates to your xml files and again create a new jar according to your need,
            You would use jar utility that comes with Java SDK for this,
            See [how to use Jar tool.|http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/jar.html]
            • 3. Re: can't update files inside a jar
              jwenting
              but that wouldn't update the files in the jar as it would create a new jar :)

              Updating files inside an archive without unpacking them and later repacking that archive is indeed not simple (though theoretically possible).
              You'd have to update the binary content of the archive, all the while keeping the structure of the archive as well as its checksums and indices correct. Some dedicated tools can achieve that, most don't bother (I think Winzip for example can do it to a degree, it can replace files in archives with new versions).
              • 4. Re: can't update files inside a jar
                Alan.M
                Hi,
                Yes but the updated binary stream needs to be persisted again
                • 5. Re: can't update files inside a jar
                  807589
                  Have anyone mentioned that updating the jar is very likely a bad idea?

                  A better idea is to have a default XML config inside the jar, if you need it, but store the modified one outside, for example:

                  path/yourapp/app.jar
                  path/yourapp/config/your jars...
                  • 6. Re: can't update files inside a jar
                    807589
                    If what you are trying to do is have a program persist configuration changes (which seems the most common reason to want to do this) you'll probably be better off using the Preferrences class. This allows you to store preferences without worrying about where they live (on Windows they are stored in the registry).
                    • 7. Re: can't update files inside a jar
                      807589
                      Sometimes I'm torn between storing data in files versus using java.util.prefs. Is it a configuration or a preference? I don't know if that question helps me, but sometimes I think that if it's okay to edit the values outside the program, that means I should go the file route.
                      • 8. Re: can't update files inside a jar
                        807589
                        But if you do use a file, it seems like a good idea to me to put the file path in Preferences, so you aren't committed to a specific location.
                        • 9. Re: can't update files inside a jar
                          807589
                          malcolmmc wrote:
                          But if you do use a file, it seems like a good idea to me to put the file path in Preferences, so you aren't committed to a specific location.
                          Next you'll want me to write a fully-qualified class name on a slip of paper, work it into a fortune cookie, and pass that to you at the end of your meal. Shhh!