7

OK. So I have a pretty simple question: I want to be able to load a resource (a whole folder) from inside a running .jar file, but I have not been able to get it to work. This is what I have tried (if the class name were "myClass" and the folder being called "myFolder"), but it always throws a NullPointerException:

URL folderURL = myClass.class.getClassLoader().getResource("myFolder/");
String folderPath = folderURL.getPath();
File myFolder = new File(folderPath);

The NullPointerException is always thrown before I create "myFolder".

Some more info: I have to access the folder from static context. The class that is accessing the folder is NOT in the same directory as the folder itself is in. (The folder is in the root directory inside the jar, the class is a couple subpackages down.)

Does anyone have a solution to my problem? Sorry if I used wrong terminology :P, but anything you can do to help is appreciated.

  • 1
    @userNNNN: "I have a pretty simple question:" 'What is the meaning of life?' is a simple question. It is the answer that is tricky. BTW - since you apparently put the files into the Jar, why not include a list of them in a single file that has a set name. Problem solved! – Andrew Thompson Jun 06 '11 at 02:16

4 Answers4

22

There's no way this will work. You're trying to create a File object from a resource inside a JAR. That isn't going to happen. The best method to load resources is to make one your package folders a resource folder, then make a Resources.jar in it or something, dump your resources in the same dir, and then use Resources.class.getResourceAsStream(resFileName) in your other Java class files.

If you need to 'brute force' the subfiles in the JAR directory pointed to by the URL given by getResource(..), use the following (although it's a bit of a hack!). It will work for a normal filesystem too:

  /**
   * List directory contents for a resource folder. Not recursive.
   * This is basically a brute-force implementation.
   * Works for regular files and also JARs.
   * 
   * @author Greg Briggs
   * @param clazz Any java class that lives in the same place as the resources you want.
   * @param path Should end with "/", but not start with one.
   * @return Just the name of each member item, not the full paths.
   * @throws URISyntaxException 
   * @throws IOException 
   */
  String[] getResourceListing(Class clazz, String path) throws URISyntaxException, IOException {
      URL dirURL = clazz.getClassLoader().getResource(path);
      if (dirURL != null && dirURL.getProtocol().equals("file")) {
        /* A file path: easy enough */
        return new File(dirURL.toURI()).list();
      } 

      if (dirURL == null) {
        /* 
         * In case of a jar file, we can't actually find a directory.
         * Have to assume the same jar as clazz.
         */
        String me = clazz.getName().replace(".", "/")+".class";
        dirURL = clazz.getClassLoader().getResource(me);
      }

      if (dirURL.getProtocol().equals("jar")) {
        /* A JAR path */
        String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); //strip out only the JAR file
        JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
        Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
        Set<String> result = new HashSet<String>(); //avoid duplicates in case it is a subdirectory
        while(entries.hasMoreElements()) {
          String name = entries.nextElement().getName();
          if (name.startsWith(path)) { //filter according to the path
            String entry = name.substring(path.length());
            int checkSubdir = entry.indexOf("/");
            if (checkSubdir >= 0) {
              // if it is a subdirectory, we just return the directory name
              entry = entry.substring(0, checkSubdir);
            }
            result.add(entry);
          }
        }
        return result.toArray(new String[result.size()]);
      } 

      throw new UnsupportedOperationException("Cannot list files for URL "+dirURL);
  }

You can then modify the URL given by getResource(..) and append the file on the end, and pass these URLs into getResourceAsStream(..), ready for loading. If you didn't understand this, you need to read up on classloading.

Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
  • `@param clazz Any java class that lives in the same place as the resources you want.` "in the same place" as in in the same JAR, or...? –  Jun 06 '11 at 02:03
  • Bear in mind that there is a difference between `getClass().getClassLoader().getResource(...)` and `getClass().getResource(...)`. The same is true for `getResourceAsStream(...)`. This looks like the 'baseless' version which uses the root of the jar as the root, essentially. Try it out -- put a resources folder in the root of your JAR and see if it can list the files in it. – Chris Dennett Jun 06 '11 at 02:10
  • Thanks to you (and Greg Briggs) for this, it's incredible how complex some file operations can be in Java. Of course this makes perfect sense now that I see the code, but before that...oh man. – Esko Jun 13 '12 at 08:09
  • 1
    Note that you can get the jar location of a class using clazz.getProtectionDomain().getCodeSource().getLocation(). – Ajax Jan 10 '13 at 23:56
1

It's pretty simple:

getClass().getResourceAsStream("/resources/images/myImage.png")

Would return an input stream which can be used like so:

Image myImage = ImageIO.read(getClass().getResourceAsStream("/resources/images/myImage.png"));

And from there, use your Image how you like. This also works just as well if you're using the input stream to read a text file, or for pretty much whatever else you're doing.

Edit: The path above is relative to the .jar's root.

kryo
  • 716
  • 1
  • 6
  • 24
  • when you say `getClass`, are you referring to `getClass()`? –  Jun 06 '11 at 02:05
  • And what if the class that is accessing the folder is in, say, myJar.myPackage.myFirstSubpackage.mySecondSubpackage and the folder is simply in myJar.myFolder? –  Jun 06 '11 at 02:07
  • OK thanks. So I could do this: `File myFolder = myClass.class.getResourceAsStream("/myFolder/")` right? wait no that wouldn't work :P –  Jun 06 '11 at 02:10
  • There *no* way you can get a File object from within a JAR. No way. The API doesn't allow for it. Get it out of your head :) – Chris Dennett Jun 06 '11 at 02:15
  • lol. So I have to do more work xD. Well... so much for that. Thanks for trying to help me though :) –  Jun 06 '11 at 02:20
  • I was referring to getClass(), sorry. And yeah, there's no way to get a File object unless it's an external file. You'll need to rethink what you're doing. Good luck! – kryo Jun 06 '11 at 02:47
0

I think I am on to something. In Eclipse, make a new source folder named "resources" and in that folder, create a package named "myFolder". To have a Path to "myFolder", you just have to say:

Path path = Paths.get(Main.class.getResource("/myFolder").toURI()); // or the class name instead of Main
Valdrinium
  • 1,398
  • 1
  • 13
  • 28
0

Up until now (December 2017), this is the only solution I found which works both inside and outside the IDE (Using PathMatchingResourcePatternResolver): https://stackoverflow.com/a/47904173/6792588

Naor Bar
  • 1,991
  • 20
  • 17