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 8699621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:59:31+00:00 2026-06-13T01:59:31+00:00

I have a use case where I need to export this specific piece of

  • 0

I have a use case where I need to export this specific piece of code as a java library (which will be a JAR eventually) but the problem is that it needs to use some piece of information stored in physical files on the file system.

I have 2 questions here:

1) Where should I put these files on the filesystem (One option that I could think of was in the resources directory of the Java module containing the library: Have a doubt though that the resources directory also gets compiled into the jar?)

2) When I am using this library from an external Java application, how would the library be able to locate the files? Would they still be in the classpath?

  • 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-06-13T01:59:33+00:00Added an answer on June 13, 2026 at 1:59 am

    I found a relevant answer as a part of another question : How to load a folder from a .jar?

    I am able to successfully retrieve the files using the following code:

       /**
       * 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);
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a use case where I need to call a (non-static) method in
I have a use case where I only need to store certain fields to
I have a strong use case for pre-allocating all the memory I need upfront
I have this use case of an xml file with input like Input: <abc
I have a use case with NServiceBus explained in this question. Essentially there is
I have the following use case: I need to process a big number of
I have a use case where I need to add information about the user
I'm new to design patterns, I have a use case where I need to
I have a use case , where I need to display some log files
We have the use case that we need to output text that is a

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.