问题
I'm using OS X, Netbeans 7.3 Beta 2, Java.
I have a program which reads from a text file. When running my distributed jar, my program does not utilise this .txt file.
Here are my two packages - ignore all java files in com.john.view apart from SPPMainGUI2.java:
As you can see, cpass.txt is found in com.john.spp. Here is how I use it:
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader("cpass.txt"));
} catch (FileNotFoundException ex) {
Logger.getLogger(SPPMainGUI2.class.getName()).log(Level.SEVERE, null, ex);
}
I'm guessing my FileReader location needs to be altered. When my program runs from Netbeans it works fine, when I run the jar file found in my dist folder, it doesn't pick up the .txt file.
Any ideas?
回答1:
In which class are you reading the file? Is it in SPPMainGUI2?
Then you'd need to change getClass()
to Main.class
or some other class that is located in the com.john.spp
package.
回答2:
Instead of reading files like with new FileReader("cpass.txt")
use the resource access mechanism:
in = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream("cpass.txt")));
来源:https://stackoverflow.com/questions/15703939/distributed-jar-file-not-picking-up-my-txt-file