0

How can i load all files from a folder in a jar file?

Untill now i was loading files from a folder like this File[] files = new File("res/Models/" + dir).listFiles(); but it doesnt in a fat jar file (i get a NullPointerException).

Note: The class that should load files and the files i want to load are in the same jar file. The line i wrote up there works when i run my program in eclipse, but when i expoort a jar file i get a NullPointerException for that line

Qualphey
  • 1,244
  • 1
  • 19
  • 44
  • 2
    Possible duplicate of [Accessing files packaged into a jar file](http://stackoverflow.com/questions/4873716/accessing-files-packaged-into-a-jar-file?rq=1) – Baz Aug 24 '12 at 21:28
  • @Baz i think you should read my question and the question you linked again :) – Qualphey Aug 24 '12 at 21:30
  • 1
    I did, but I don't see the difference. Maybe you can explain... – Baz Aug 24 '12 at 21:32
  • I want to load all files from a folder! – Qualphey Aug 24 '12 at 21:34
  • 2
    As explained in the link Baz gave, you will need to extract the contents of the file first. – Code-Apprentice Aug 24 '12 at 21:35
  • 1
    @Code-Guru Thank you. Maybe now it's more obvious, why this is a duplicate... – Baz Aug 24 '12 at 21:38
  • Do you see any difference between a FILE and ALL FILES IN A FOLDER? – Qualphey Aug 24 '12 at 21:50
  • 2
    @Chorche Yes. However, inside of a jar file there is no such thing as a "file" or a "folder". In order to reconstruct these things, you need to extract the contents of the Jar file. – Code-Apprentice Aug 24 '12 at 22:46
  • *"How can i load all files from a folder in a jar file?"* Using 'Fat Jar' is not usually the best way to implement things. It is better to just add the dependent Jars to the run-time class-path of the application. But the fact you are trying to find a list of those Jars is itself indicating you are very confused. What user feature are you trying to implement through all this? E.G. 'Can extend program using plug-ins!', or (what?).. – Andrew Thompson Aug 25 '12 at 00:13
  • If you don't really have to get the actual files and it is sufficient with an `InputStream` then you can take a look at my answer here: http://stackoverflow.com/questions/12015747/get-all-images-within-directory-within-jar-file/12016222#12016222 – maba Aug 26 '12 at 18:29

2 Answers2

2

A jar file is a zip file, so you can read its contents using a ZipInputStream like this:

  ZipFile zipFile = new ZipFile(file);
  ZipInputStream stream = new ZipInputStream(new BufferedInputStream(new FileInputStream(file)));
  ZipEntry entry = null;
  List<String> filePaths = new ArrayList<String>();
  while ((entry = stream.getNextEntry()) != null) {
    // this will get you the contents of the file
    InputStream inputStream = zipFile.getInputStream(entry);
    // this add the file path to the list
    filePaths.add(entry.getName());
  }

You can also do the follo

Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • Can the "file" in this line `ZipInputStream stream = new ZipInputStream(new BufferedInputStream(new FileInputStream(file)));` be a directory? – Qualphey Aug 24 '12 at 21:47
  • No, but you can have another method that recursively goes through the directory and if it finds a .zip file it calls the code I gave you, otherwise it does whatever you want it to do. – Dan D. Aug 24 '12 at 21:51
  • so i have to place my files into a zip archive? I think you dont understand what my question is. The class that should load files and the files i want to load are in the same folder. The line i wrote in my question works when i run my program in eclipse, but when i expoort a jar file i get a NullPointerException for that line. You should see this question http://stackoverflow.com/questions/12116151/fat-jar-instantly-closes/12116376 – Qualphey Aug 24 '12 at 21:58
  • In this case, I think nobody understood what you asked. You should take your time to write questions that others can understand. – Dan D. Aug 24 '12 at 22:00
  • Oops, i ment that the class that should load files and the files i want to load are in the same jar file – Qualphey Aug 24 '12 at 22:06
  • I edited my answer. It shows you now how to read the contents of the files inside the jar/zip file. You will have to loop through the jar file and if the file path matches to what you are looking for, read its contents. – Dan D. Aug 24 '12 at 22:12
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/15787/discussion-between-chorche-and-dan) – Qualphey Aug 24 '12 at 22:17
0

Let's look at how a File object works.

First, say we have the following code in a file named FileTest.java:

public class FileTest {
    public static void main() {
        File[] files = new File("temp").listFiles();

        for (File f : files) System.out.println(f.getName());
    }
}

When we compile this, javac creates a file named FileTest.class. (Even Eclipse will do this; it just places the .class files in a separate folder from the .java files.) Now when we run this program, it will look for a folder named "temp" located in the same folder as the FileTest.class file.

Now suppose that we create a JAR file named filetest.jar and add FileTest.class to it. Now if we run the program from the JAR file, it will look for a folder named "temp" located in the same folder as the filetest.jar file. In other words, File only looks for files and folders in the file system on your hard drive. It knows absolutely nothing about the contents of any files, including filetest.jar.

In order to read the contents of a JAR file, you must find a different solution. To start, you need to understand that inside a JAR file there is no such thing as "files" and "folders". A JAR file is simply a ZIP file with a few extra features. With the information you have given here so far, I believe that to solve your problem you will need to use java.util.zip.ZipFile. I suggest you start by googling for a tutorial or browing the API doc which I have linked here.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268