0

I have been given an existing program that works fine in Eclipse and I want to compile it for distribution. I have no problem in accessing individual resource files both in Eclipse and from a jar.

However, the program iterates through a bunch of files in a folder and I can't figure out how to replicate the logic so it works both in Eclipse and from a jar.

Some of the original code, as requested:

File[] baseFiles = baseFolder.listFiles();
for (int i = 0; i < baseFiles.length; i++) {
    String fileName = baseFiles[i].getName();
    if (!baseFiles[i].isDirectory() && fileName.toUpperCase().endsWith(XML)) {
        // process file
    }
}

I tried a few things . . . didn't record them all. This one gives me an empty enum:

    final Enumeration < URL > oneTime = getClass ( )
          .getClassLoader ( )
          .getResources ( basefolder ) ;

In response to other comments - in particular whether this is a duplicate of other questions - I have modified the Title to make it clearer

  • Can you provide more details? What is the "logic" and what have you tried doing thus far? – Keith Sep 28 '15 at 18:42
  • 1
    Please show use some code. There are dozens of ways to load files or resources in Java. We cannot help without code (and preferable with some exception). – Thomas Uhrig Sep 28 '15 at 19:00
  • 1
    And welcome to Stackoverflow! – Thomas Uhrig Sep 28 '15 at 19:01
  • The short answer is, it's difficult (and has been asked a number of times before). Essentially, you need to create a text file which contains the path/name of the files in your jar(s). This can built dynamically at build time or you can manage it manually. The at runtime, you would read this file using Class#getResource, now you have a list of the files – MadProgrammer Sep 28 '15 at 21:58
  • eew! I'm amazed that this is not standard - after all it should be equally as simple as It is to access a single file as a resource - and that's transparent as to whether you're executing in Eclipse or from a jar. – Hugh Lapham Sep 29 '15 at 19:38
  • What is the error you get? Please update the question with some more details. – jeff porter Sep 30 '15 at 13:46
  • Duplicate of http://stackoverflow.com/questions/1463192/reading-content-of-a-jar-file-at-runtime? – James Jithin Sep 30 '15 at 13:53

1 Answers1

0

Based on what I've seen (and in particular, MadProgrammer's rep) the answer is:

Essentially, you need to create a text file which contains the path/name of the files in your jar(s). This can built dynamically at build time or you can manage it manually. The at runtime, you would read this file using Class#getResource, now you have a list of the files