MRedgate
MRedgate

Reputation: 11

File gives null pointer after being exported to a jar

In my code I have need populate a JComboBox by scanning a directory and feeding the results to the Combo box.

I do this as follows

File folder = new File("src/profiles");
for (final File fileEntry : folder.listFiles())             
         list.add(fileEntry.getName().substring(0, fileEntry.getName().length()-5));

This works just fine in Eclipse, but once I export to a runnable jar it throws a null pointer exception on my for loop. I'm guessing this is because the folder file can't be made/found since the structure is no longer the same. I've checked the jar with 7zip and the profiles dir is there. It's at the root of the jar, but even if I change "src/profiles" to "profiles" or "\profiles" it still throws the same null pointer exception error

The Directory structure looks something like this for reference

    Project
       bin
          name
          images
          profiles
       src
          name
              this.java
              that.java
          images
          profiles
              profile1.json
              profile2.json

Anyone either know of a way still make this work after exporting to a jar? either by getting the folder in a different way or scanning the directory in another way?

Upvotes: 1

Views: 474

Answers (1)

marciopd
marciopd

Reputation: 130

If you want to look up for resources in the application's classpath (inside the jar in your case) then you can use Java's classloader resources features.

Here's an example: Get a list of resources from classpath directory

Upvotes: 0

Related Questions