Distributed jar file not picking up my .txt file

Deadly 提交于 2019-12-25 04:55:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!