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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:39:18+00:00 2026-05-28T20:39:18+00:00

I have a set of files in my Maven resource folder: + src +

  • 0

I have a set of files in my Maven resource folder:

+ src
  + main
    + resources
      + mydir
        + myfile1.txt
        + myfile2.txt

How can I iterate mydir? Not only in Eclipse, but when running JUnit tests from the command line, and from a dependent jar.

File mydir = new File("mydir");
for (File f : dir.listFiles()) {
   dosomething...       
}

Thanks for a hint!

  • 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-28T20:39:19+00:00Added an answer on May 28, 2026 at 8:39 pm

    In the end, this is what I came up with to handle accessing files within referenced jars:

    public class ResourceHelper {
    
        public static File getFile(String resourceOrFile)
            throws FileNotFoundException {
        try {
    
            // jar:file:/home/.../blue.jar!/path/to/file.xml
            URI uri = getURL(resourceOrFile).toURI();
            String uriStr = uri.toString();
            if (uriStr.startsWith("jar")) {
    
            if (uriStr.endsWith("/")) {
                throw new UnsupportedOperationException(
                    "cannot unjar directories, only files");
            }
    
            String jarPath = uriStr.substring(4, uriStr.indexOf("!"))
                .replace("file:", "");
            String filePath = uriStr.substring(uriStr.indexOf("!") + 2);
    
            JarFile jarFile = new JarFile(jarPath);
            assert (jarFile.size() > 0) : "no jarFile at " + jarPath;
    
            Enumeration<JarEntry> entries = jarFile.entries();
    
            while (entries.hasMoreElements()) {
    
                JarEntry jarEntry = entries.nextElement();
                if (jarEntry.toString().equals(filePath)) {
                InputStream input = jarFile.getInputStream(jarEntry);
                assert (input != null) : "empty is for " + jarEntry;
                return tmpFileFromStream(input, filePath);
                }
            }
            assert (false) : "file" + filePath + " not found in " + jarPath;
            return null;
            } else {
            return new File(uri);
            }
    
        } catch (URISyntaxException e) {
            throw new FileNotFoundException(resourceOrFile);
        } catch (IOException e) {
            throw new FileNotFoundException(resourceOrFile);
        }
        }
    
        private static File tmpFileFromStream(InputStream is, String filePath)
            throws IOException {
    
        String fileName = filePath.substring(filePath.lastIndexOf("/") + 1,
            filePath.lastIndexOf("."));
        assert (fileName != null) : "filename cannot be null for " + filePath;
        String extension = filePath.substring(filePath.lastIndexOf("."));
        assert (extension != null) : "extension cannot be null for " + filePath;
    
        File tmpFile = File.createTempFile(fileName, extension);
        // tempFile.deleteOnExit();
        assert (tmpFile.exists()) : "could not create tempfile";
    
        OutputStream out = new FileOutputStream(tmpFile);
        int read = 0;
        byte[] bytes = new byte[1024];
        while ((read = is.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        is.close();
        out.flush();
        out.close();
        assert (tmpFile.length() > 0) : "file empty "
            + tmpFile.getAbsolutePath();
        return tmpFile;
        }
    
        public static File getTempFile(String resourceOrFile) throws IOException {
    
        InputStream input = getInputStream(resourceOrFile);
    
        File tempFile = IOUtils.createTempDir();
        tempFile.deleteOnExit();
        FileOutputStream output = new FileOutputStream(tempFile);
    
        byte[] buffer = new byte[4096];
        int bytesRead = input.read(buffer);
        while (bytesRead != -1) {
            output.write(buffer, 0, bytesRead);
            bytesRead = input.read(buffer);
        }
        output.close();
        input.close();
    
        return tempFile;
        }
    
        public static InputStream getInputStream(String resourceOrFile)
            throws FileNotFoundException {
    
        try {
            return getURL(resourceOrFile).openStream();
        } catch (Exception e) {
            throw new FileNotFoundException(resourceOrFile);
        }
        }
    
        public static URL getURL(String resourceOrFile)
            throws FileNotFoundException {
    
        File file = new File(resourceOrFile);
        // System.out.println("checking file ");
        // is file
        if (file.exists()) {
            // System.out.println("file exists");
            try {
            return file.toURI().toURL();
            } catch (MalformedURLException e) {
            throw new FileNotFoundException(resourceOrFile);
            }
        }
        // is resource
        if (!file.exists()) {
            // System.out.println("file resource");
            URL url = Thread.class.getResource(resourceOrFile);
            if (url != null) {
            return url;
            }
            url = Thread.class.getResource("/" + resourceOrFile);
            if (url != null) {
            return url;
            }
        }
        throw new FileNotFoundException(resourceOrFile);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of .php files in a folder, I want to add
In my project, I have a resources directory ( src/main/resources ) that contains properties
I have two WARS: base-overlay example-app base-overlay has XML config files in src/main/webapp/WEB-INF/spring/*.xml .
I have JAVA_HOME variable set to C:\Program Files\Java\jre6\ when I run maven package on
I have my JAVA_HOME set to: C:\Program Files (x86)\Java\jdk1.6.0_18 After I run maven install
I have a set of files. The set of files is read-only off a
I have a set of files in a single directory named: OneThing-x.extension OneThing-y.extension OneThing-z.extension
I have a large set of files, some of which contain special characters in
I have a set of zip files which contains several ieee-be encoded binary and
I have a set of html files that I want to modify by replacing

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.