1

I have a large-ish project that contains one class that reads from a file (I have enclosed an SSCCE below). Everything works as it should. However, when I use either the Apple Jar Bundler or Eclipse's "Export to Mac OS X Application command" (following these instructions), it doesn't work, and I get a java.io.FileNotFoundException.

I am trying to find out why I get this FileNotFoundExceptionand how I can prevent it. My guess is that Eclipse is using its own classloader or something and that said classloader is not being properly invoked in the exported jar and hence the app.

SSCCE: The following code works when run from Eclipse, but not from a .app, java -jar, or even from the java readfromfile.ReadFromFile executed from the bin directory.

In ReadFromFile.java:

package readfromfile;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ReadFromFile {
    public static void main(String[] args) {
        String filepath = "src/readfromfile/file.txt";
        try {
            BufferedReader br = new BufferedReader(new FileReader(filepath));
            JFrame frame = new JFrame();
            frame.getContentPane().setLayout(
                    new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
            for (String line; (line = br.readLine()) != null;) {
                frame.getContentPane().add(new JLabel(line));
            }
            frame.pack();
            frame.setVisible(true);
        } catch (FileNotFoundException e) {
            System.err.println("File not found");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("IOexception");
            e.printStackTrace();
        }
    }
}

In file.txt:

I am text
Community
  • 1
  • 1
wchargin
  • 15,589
  • 12
  • 71
  • 110

1 Answers1

3

When you create the Jar Bundle maybe the src/... path does not exist.

Why don't you put the file as packages resource and you read it with:

Reader r = new InputStreamReader(ReadFromFile.class.getResourceAsStream("file.txt"));
BufferedReader br = new BufferedReader(r);

Naturally you have to put the file "file.txt" in the same package of your class.

dash1e
  • 7,677
  • 1
  • 30
  • 35
  • 1
    *"packages resource"* I generally refer to it as an 'embedded application resource'. ( I'm trying to start a trend. ;) *"Naturally you have to put the file "file.txt" in the same package of your class."* I generally find it safer to refer to the resource by 'full path from the root of the class-path'. In this case that might be along the lines of `"/readfromfile/file.txt"`. +1 for recognizing that this resource might not be accessible as a `File`. – Andrew Thompson Apr 09 '12 at 14:55
  • 2
    @AndrewThompson you are right, but I wanted to suggest a very simple solution to start with, however I vote your comment! – dash1e Apr 09 '12 at 15:03
  • So, would `file.txt` be located in the same directory as `ReadFromFile.java`? Is that what you mean by "in the same package of your class"? – wchargin Apr 09 '12 at 21:03