4 Replies Latest reply: Jul 18, 2007 6:53 PM by 807605 RSS

    Confused about "files inside a JAR"

      I have a program that has a Properties file which I would like to "save" into the JAR file, so it is not outside the JAR.

      I know this can be done, because I've seen some Java programs that save their configuration between sessions, but not via a file inside its directory. This leads me to believe the file is maintained within the JAR itself, which would be REALLY handy and secure.

      Is this a correct assumption? If so, how would I do it? Here's a "test program" that we can play with, how would I save MyProperties' properties field into the MyProgram JAR?
      package SBX;
      import java.util.*;
      import java.io.*;
      public class MyProgram {
          // Instance Variables
          public MyProperties myProp = new MyProperties();
          public String stringOne = "Will this be loaded?";
          public String stringTwo = "How about this one?";
          // Main Method
          public static void main(String[] args) {
              new MyProgram();
          // Constructors
          public MyProgram() {
              if ((new File("properties.txt")).exists()) {
              System.out.println("1) " + stringOne);
              System.out.println("2) " + stringTwo);
              System.out.println("3) " + stringOne);
              System.out.println("4) " + stringTwo);
              // -- //
              // Now that we've "updated" stringOne & stringTwo and "updated" myProp and saved it,
              // we should be able to get the values "ONE" and "TWO" for print outs 1 & 2 when we next
              // run the program, instead of "Will this be loaded?" and "How about this one?", as we got during the first run.
              // However, the thing I need help with is saving the Properties to the JAR file so it's not accessable
              // outside of the JAR file. How can this be done?
      class MyProperties {
          // Instance Variables
          private Properties properties;
          // Constructors
          public MyProperties() {
              Properties defaults = new Properties();
              defaults.setProperty("stringOne", "ONE");
              defaults.setProperty("stringTwo", "TWO");
              properties = new Properties(defaults);
              try {
                  FileInputStream fis = new FileInputStream("properties.txt");
              catch (Exception e) {
                  System.out.println("No properties file found, ignore this; one will be saved when the program exits.");
          // Methods
          public void updateProgram(MyProgram prg) {
              prg.stringOne = properties.getProperty("stringOne");
              prg.stringTwo = properties.getProperty("stringTwo");
          public void updateProperties(MyProgram prg) {
              properties.setProperty("stringOne", prg.stringOne);
              properties.setProperty("stringTwo", prg.stringTwo);
              try {
                  FileOutputStream fos = new FileOutputStream("properties.txt");
                  properties.store(fos, "A header");
              catch (Exception e) {
      Thanks for any help!
      PS...writing demo programs is kind of fun :)
        • 1. Re: Confused about "files inside a JAR"
          You can't modify files in a JAR. Look at Preferences.
          • 2. Re: Confused about "files inside a JAR"

            Yup... you can jar up your properties file... you just need to add it to manifest file and jar (the program) takes care of the rest... google that there's loads of existing articles.

            BUT, I don't think you can modify the properties file in the jar... it may be possible, but I just don't know how. Instead (I think) you ship your default properties file in the jar, but still save a local properties file to the same directory as the jar (obviously won't work for applets)... then you load your defaults, and the local settings over the top.

            and PS I don't see how sticking a properties file in the jar would "secure" it in any fashion... it's just a zip file after all... would you care to elaborate?


            • 3. Re: Confused about "files inside a JAR"
              Thanks for the replies!

              I'm checking out the Preferences class right now.

              And corlettk, what I think I was trying to get at with more secure was that it wouldn't be easily viewable / openable by a user. That, and it will make the program's directory look cleaner. I'll also try your suggestion, though, and read up about this manifest creature. I feel stupid not knowing fully what it is!

              • 4. Re: Confused about "files inside a JAR"
                I wish I had known about Preferences much earlier than this thread, it would have saved me a ton of work on a ton of things.

                Thanks for the help. :)