0

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:

Ignore all

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?

John Vasiliou
  • 977
  • 7
  • 27
  • 48

2 Answers2

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")));
Joni
  • 108,737
  • 14
  • 143
  • 193
  • OK, I've changed the reference to ("../cpass.txt") but now it keeps throwing many errors such as Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Unclosed character class near index 11 qwertyuiop[] - I don't want to have to go through the .txt file and fix all these errors. Any idea? – John Vasiliou Mar 29 '13 at 13:19
  • From the error it looks like you are constructing regular expressions from strings that are not regular expressions, which has nothing to do with reading the file. Maybe you should post a new question with the details of this new error. – Joni Mar 29 '13 at 13:21
  • I have removed the words in the .txt file causing the errors. It runs perfectly in Netbeans now, but doesn't run at all as a dist jar file now. Any ideas? – John Vasiliou Mar 29 '13 at 13:25
  • How do you run it? Command line? Double click in Explorer on Windows? – Joni Mar 29 '13 at 13:27
  • I am using OS X, I find the folder where all my documents for this program are stored, locate the dist folder and double click on the jar file. – John Vasiliou Mar 29 '13 at 13:29
2

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.

skirsch
  • 1,640
  • 12
  • 24
  • It is SPPMainGUI2. When I change it, I get an error which reads "Cannot find symbol". – John Vasiliou Mar 29 '13 at 13:26
  • Well, that could be anything. Is this a compile error or does it occur when you run it? – skirsch Mar 29 '13 at 13:30
  • A compilation error. To clarify, I have written it like so: Main.class.getResourceAsStream("../cpass.txt") I have also tried "cpass.txt" – John Vasiliou Mar 29 '13 at 13:32
  • 1
    Did you import the `Main` class? And then, you need to omit the `../` part of the path. Or you stick to the getClass() along with the `../` - it will have the same effect. – skirsch Mar 29 '13 at 13:37
  • Amazing. It works, thanks a lot. Is it bad practice to do this from within a GUI package? – John Vasiliou Mar 29 '13 at 13:39
  • Both of these questions helped, the first helped me start off and this helped fixed the final error. Which do I choose? – John Vasiliou Mar 29 '13 at 13:39
  • 1
    I wouldn't call it bad practice. If you'd load more resources, I'd put all the stuff in a specific folder and have some Resources class that loads all the stuff and use that class as a reference. But just for one file... it's not that relevant I guess. – skirsch Mar 29 '13 at 13:45