2 Replies Latest reply: Aug 10, 2010 2:41 AM by 843789 RSS

    How to read a file inside the JAR file

    843789
      Hi All,

      I want to save some preferences in a file called "preferences". I kept the preferences file under my java package.
      If i am running the code inside the NetBeans 6.9 IDE it's working file. But once i have created a JAR and try to run the application, it couldn't find the path.
      Please help me that how to resolve this issue. I don't want to save this preferences file outside of my JAR (i.e) within my java package.

      Here is the code,
      package mypackage;
      
      import java.io.BufferedReader;
      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      import java.io.InputStreamReader;
      import java.io.OutputStream;
      import java.io.PrintWriter;
      
      public class ReadFile
      {
          private void writeFile()
          {
              try
              {
                  OutputStream out = new FileOutputStream(getClass().getClassLoader().getResource("mypackage/preferences").getPath().replace("%20", " "));
                  PrintWriter writer = new PrintWriter(out);
                  writer.println("Hello Java!");
                  writer.close();
                  out.close();
              }
              catch (Exception ex)
              {
                  System.out.println(ex.getMessage());
              }
          }
      
          private void readFile()
          {
              try
              {
                  FileInputStream fi = new FileInputStream(getClass().getClassLoader().getResource("mypackage/preferences").getPath().replace("%20", " "));
                  BufferedReader br = new BufferedReader(new InputStreamReader(fi));
                  System.out.println(br.readLine().trim());
                  br.close();
                  fi.close();
              }
              catch (Exception ex)
              {
                  System.out.println(ex.getMessage());
              }
          }
      
          public static void main(String[] args)
          {
              ReadFile read = new ReadFile();
              read.writeFile();
              read.readFile();
          }
      }
      If i run the JAR, i get the following error message,
      {color:#ff0000}*file:\my jar path\jarname.jar!\mypackage\preferences (The filename, directoryname, or volume label syntax is incorrect)*{color}
        • 1. Re: How to read a file inside the JAR file
          843789
          You cannot update a running jar file. There are two ways I approach this depending on my exact requirements -

          1) use the java.util.prefs.Preferences API

          2) if not already present I copy the preferences file from the jar to a known place. I use a directory in the user's home directory and normally make the directory name the program with a '.' prefix.

          One cannot access files in a jar file using the File API. One needs to use the getResource() or getResourceAsStream() methods on Class or ClassLoader.
          • 2. Re: How to read a file inside the JAR file
            843789
            Thanks sabre150,
            sabre150 wrote:
            You cannot update a running jar file.
            I am sorry. I didn't know about it. Thanks to point out.
            There are two ways I approach this depending on my exact requirements -
            1) use the java.util.prefs.Preferences API
            I will try this one

            >
            2) if not already present I copy the preferences file from the jar to a known place. I use a directory in the user's home directory and normally make the directory name the program with a '.' prefix.

            One cannot access files in a jar file using the File API. One needs to use the getResource() or getResourceAsStream() methods on Class or ClassLoader.