Search code examples
javajarnullpointerexceptionfilereaderfilewriter

JAR file that reads .cfg file and produces a text file


I developed a small program that takes as input some configuration parameters from a .cfg file and produces an output .txt file based on the values taken from the .cfg file.

While the program runs perfectly in eclipse, I receive a NullPointerException error when I create a JAR file of this program and try to run it. From my understanding I have to make the JAR access its internal files or try to receive the needed information (in this case the .cfg file) externally, e.g. create a resource folder next to the JAR file.

I have searched many related questions asked here but I got even more confused whether there is an optimal way to produce a JAR file that can access input files and produces output files. Should I modify my code to achieve this or there is another way?

For the record, I use FileReader and FileWriter to access and produce the files.


Solution

  • If your .cfg file is outside of the JAR file, it should work just as in Eclipse.

    If you want to access it from inside of the JAR archive, then you should use a class loader to load it, instead of FileReader...