Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5988189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:55:26+00:00 2026-05-22T22:55:26+00:00

OK. So I have a pretty simple question: I want to be able to

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:55:27+00:00Added an answer on May 22, 2026 at 10:55 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pretty simple question: When i have a persistable object, it usually has a property
Should be pretty simple: I have an InputStream where I want to peek at
I have a pretty simple question (and these are typically the ones I spend
I have a simple console app written in C#. I want to be able
I have what I think is a pretty simple question. I have a report
Pretty simple scenario. I have a web service that receives a byte array that
I am pretty new to php, but I am learning! I have a simple
I have pretty big background of .net, and I've decided that i want to
I think this is a pretty simple question but I'm having trouble finding the
I am writing a pretty simple CMS on GAE, and I want my users

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.