9 Replies Latest reply: Sep 7, 2008 7:09 AM by 807589 RSS

    How can I access a directory witin a jar file?

    807589
      Hi, I'm using java web start and I can access the images in my jar file by using the standard getClass().getResource( ... ) method. However trying to get a File object of a directory. This is because I want to search the directory for files that it contains and list them on the screen. Can anyone please tell me how I can get a File object of a directory within my jar file or how to get a list of all the files within that directory, many thanks, Anthony.
        • 1. Re: How can I access a directory witin a jar file?
          800322
          There are no files. All you have are a few entry sets in a JAR file that become files when you extract and write them, so you will never get a File object directly from a JAR. You'd have to use the JarFile class for that and work through the entries.
          • 2. Re: How can I access a directory witin a jar file?
            807589
            that's great, thank you I will look into that.
            • 3. Re: How can I access a directory witin a jar file?
              807589
              Hi I've been looking everywhere and trying lots of things to try and get access to the jar file while I'm actually running inside of it and I can't find a way. The code below needs a location of the jar file but I can't find out how to get this information while i'm running inside the jar. I'm using java web start so the application will be loaded onto a user machine and could be run from a variation of places.
                       JarFile jf = new JarFile( f );
              • 4. Re: How can I access a directory witin a jar file?
                807589
                (need to add a comment to mark as unanswered)
                • 5. Re: How can I access a directory witin a jar file?
                  800322
                  fitz29 wrote:
                  Hi I've been looking everywhere and trying lots of things to try and get access to the jar file while I'm actually running inside of it and I can't find a way.
                  You have an interesting understanding of stuff. You're not running "in a JAR file". You're running in RAM with resources read from the JAR.
                  The code below needs a location of the jar file but I can't find out how to get this information while i'm running inside the jar.
                  The information won't really be available anyway, because your JAR ceases to exist in the case of Java Web Start. If you ever had a look at the JWS cache, it'll contain a bunch of JARs, but none of them is the roiginal one.
                  I'm using java web start so the application will be loaded onto a user machine and could be run from a variation of places.
                  Then you're pretty much SOL. With a regular JAR, you could have added some "anchor" resource which you know, get its URL by getResource() and then parse the URL string for the JAR's location. With JWS mangling the JARs, this may not work.

                  You'll probably have to provide a list.

                  I'm sorry, I didn't quite think of this reading your first post.
                  • 6. Re: How can I access a directory witin a jar file?
                    807589
                    Yes I've tried to getResource and then parse the location but unforunately it returns the remote location of the jar in the file something along the lines of http://www.myurl.com/myJar.jar!/Images/myImage.jpg. Unfortunately this hasn't seemed to help at all, however I'm still silently hopeful there's something out there as the getResource() can navigate the jar then surely there must be a way of doing it, and hopefully in a less restrictive way.
                    • 7. Re: How can I access a directory witin a jar file?
                      800322
                      Hm, I wasn't aware that JWS isn't necessarily downloading stuff. Maybe for online-only apps... it's been years since I last used it.

                      Sure getResource can still load resources from there. See UrlClassLoader. Which won't immediately help you, unless you look at its sources for the resource-finding mechanism.
                      • 8. Re: How can I access a directory witin a jar file?
                        807589
                        I think that you are right and that it doesn't actually go back to the url location to retrieve the file. I'd hazard a guess that it's more of a location label for the files of the jar, but I will keep on looking into it.
                        • 9. Re: How can I access a directory witin a jar file?
                          Darryl Burke
                          Cross posted and answered in the Java Archive (JAR) Files forum.
                          [http://forums.sun.com/thread.jspa?threadID=5329366]

                          Cross posting is rude.

                          db