0

My apologies if this is a duplicate, I've been searching around and haven't found anything that works.

I've been trying export a project as a JAR file that includes reading information from a text file. After doing some research, I changed my reader from FileReader to InputStreamReader, using CLASSNAME.class.getClassLoader().getResourceAsStream("textFile.txt"). (I also understand that it should work without the getClassLoader() method involved) However, getResourceAsStream("textFile.txt") returns null, throwing a NullPointerException when I try to read it using a BufferedReader.

From what I've read, this is because my text file isn't actually in the JAR. Yet when I attempt to do so I still get a NullPointerException. I've also tried adding the folder with the files to the build path, but that doesn't work either. I'm not sure how to check if the files are actually in the JAR and, if not, how to get them in the JAR so they can be found and properly read.

For reference, I currently use Eclipse Neon on a MacBook Air and here is my code that tries, but fails, to read the text file:

public static void addStates(String fileName) {
        list.clear();
        try {
            InputStream in = RepAppor.class.getClassLoader().getResourceAsStream("Populations/" + fileName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            /*
             * NOTE: A Leading slash indicates the absolute root of the directory, which is on my system
             * Don't use a leading slash if the root is relative to the directory
             */
            String line;
            while(!((line = reader.readLine()) == null)) {
                list.add(line);
        }
        reader.close();
    } catch (IOException e) {
        JOptionPane.showMessageDialog(null, "The file, " + fileName + ", could not be read.", "Error", JOptionPane.ERROR_MESSAGE);
    } catch (NullPointerException n) {
        JOptionPane.showMessageDialog(null, "Could not find " + fileName + ".\nNull Pointer Exception thrown", "Error", JOptionPane.ERROR_MESSAGE);
    }
}

Thank you for your consideration and I appreciate and welcome any feedback you might have.

Saheb
  • 1,392
  • 3
  • 13
  • 33
kairom13
  • 7
  • 4

1 Answers1

1

There are a number of ways to check the contents of a .jar file.

Most IDEs have a “Files” section where you can simply expand a .jar file, as if it were a directory.

If your have the JDK’s bin subdirectory in your execution path, you can use the jar command in a terminal:

jar tf /Users/AaronMoriak/repappor.jar

Every .jar file is actually a zip file with a different extension (and one or more Java-specific special entries). So, any command that handles zip files will work on .jar files.

Since you’re on a Mac, you have access to the Unix unzip command. In a terminal, you can simply do this:

unzip -v /Users/AaronMoriak/repappor.jar

(The -v option means “view but don’t extract.”)

If your .jar file has a lot of entries, you can limit the output of the above command:

unzip -v /Users/AaronMoriak/repappor.jar | grep Populations

Your code comment about a leading slash is not quite correct. However, if you remove the getClassLoader() part, the comment is somewhat more correct:

// Change:
// RepAppor.class.getClassLoader().getResourceAsStream
// to just:
// RepAppor.class.getResourceAsStream

// Expects 'Populations' to be in the same directory as the RepAppor class.
InputStream in = RepAppor.class.getResourceAsStream("Populations/" + fileName);

// Expects 'Populations' to be in the root of the classpath.
InputStream in = RepAppor.class.getResourceAsStream("/Populations/" + fileName);
VGR
  • 40,506
  • 4
  • 48
  • 63