I have a file holding default information that I use to load the textFields of my application. I looked up how to get this built into my jar file when I build and I was told to put it in the source packages and it would be brought along, so I have done that.
File Structure:
Project
-Source Packages
-src
~Java Classes
-defaultFiles
~Defaults.txt
The code I am trying to use is this:
BufferedReader in;
try {
URL resourceURL = FuelProperties.class.getResource("/defaultFiles/Defaults.txt");
in = new BufferedReader(new FileReader(resourceURL.getPath()));
}
And this works perfectly when I run it through NetBeans but when I build the project and try to run it from the jar file it is not grabbing the file.
I have verified that the default file is being built and exists in the same file structure shown above.
If you can help me out with this I would be extremely grateful as I have no idea what is keeping this from working. Thanks.
I found the answer after searching through a couple dozen questions. It turns out that you can only get a
InputStreamof the data within a file within your JAR not aFileobject like I was attempting to do.(If you want the
Fileobject you just have to extract the files from the JAR in your program and then you have access to it.)So the code that got my problem to work was simply replacing this:
With this:
And now it is working both inside NetBeans and in the Built JAR file.