4 Replies Latest reply: Sep 8, 2009 3:10 AM by 807580 RSS

    Loading resources from jar files

    807580
      Hi.
      I have a problem with loading resources from within my jar file

      In my current program i load both images and text files from a jar file with the directories 'Images' (containing the images) and 'Maps' (containing the text files).

      For loading Images i use the following code:

      ImageIcon(getClass().getResource("Images/imageFile.png")).getImage();

      and it works perfectly.

      For loading Text files, i use the following code:

      getClass().getResource("Maps/textFile.txt").getPath();

      yet it produces a java.io.FileNotFoundException, i was wondering if there was another way of loading the text file or if i was doing it wrong.

      Any help would be appreciated.
      Cheers fellas, javaw0v.
        • 1. Re: Loading resources from jar files
          807580
          File paths are not really valid when the file is inside a jar. Rather than use getResource() use getResourceAsStream() and process the InputStream directly.
          • 2. Re: Loading resources from jar files
            807580
            By the way, if the Images and Maps directories are relative to the root of the jar then you should use getClass().getResourceAsStream("/Maps/textFile.txt"). If you don't use a leading slash then Class.getResource gets a path relative to the package of the class.

            getResource from a jar doesn't produce a file:// URL at all, but a special jar:// URL which specifies the jar file and the entry. You can call openStream() on the URL or use getResourceAsStream but you can't process a jar entry as a file, because it isn't a file.
            • 3. Re: Loading resources from jar files
              807580
              sorry, but i dont understand what you mean by processing the "InputStream directly"
              Atm i am using a method which accepts a string for the file path
              loadmap("Maps/level1.txt");
              i can run this anywhere where i use my jar, but it has to be in the same directory as the folder "Maps" with the txt file in it and just to repeat my problem, i would like to be able to locate this file from inside my jar file.
              • 4. Re: Loading resources from jar files
                807580
                javaw0v wrote:
                sorry, but i dont understand what you mean by processing the "InputStream directly"
                Atm i am using a method which accepts a string for the file path
                loadmap("Maps/level1.txt");
                i can run this anywhere where i use my jar, but it has to be in the same directory as the folder "Maps" with the txt file in it and just to repeat my problem, i would like to be able to locate this file from inside my jar file.
                You can't use this method with a jar file entry (resource or otherwise) because a jar file entry does not have a file path.

                That's why library classes like ImageIcon take a URL rather than a file path.

                If you can, change the signature of loadmap to take a URL (or add an equivalent method with a URL parameter). If you can't your only way to use a resource here would be to copy it onto a temporary file and pass the file path of that.