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