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

10
  • 2
    Possible duplicate of Accessing files packaged into a jar file Commented Aug 24, 2012 at 21:28
  • 1
    I did, but I don't see the difference. Maybe you can explain... Commented Aug 24, 2012 at 21:32
  • 2
    As explained in the link Baz gave, you will need to extract the contents of the file first. Commented Aug 24, 2012 at 21:35
  • 1
    @Code-Guru Thank you. Maybe now it's more obvious, why this is a duplicate... Commented Aug 24, 2012 at 21:38
  • 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. Commented Aug 24, 2012 at 22:46

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

7 Comments

Can the "file" in this line ZipInputStream stream = new ZipInputStream(new BufferedInputStream(new FileInputStream(file))); be a directory?
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.
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 stackoverflow.com/questions/12116151/fat-jar-instantly-closes/…
In this case, I think nobody understood what you asked. You should take your time to write questions that others can understand.
Oops, i ment that the class that should load files and the files i want to load are in the same jar file
|
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.