0

I am trying to export a GUI (Student Grade Manager App) I created in eclipse as a JAR file and run it on my desktop. Nothing happens when I double click it (though it does successfully export) at the moment. Here's an screenshot:

GUI Screenshot - Click here

I'm assuming the main issue here is that in my GUI, I am using the File Input stream in the CourseSectionApp.java file (Where the main method is located). It reads a .txt file called "StudentMarks.txt" and that's where it gets all its student data from.:

 BufferedReader aFile = new BufferedReader ( new FileReader("Marks.txt"));
     model = CourseSection.loadFrom(aFile);

Is there anyway to get this to work? Can I have the .txt file just export with the JAR file so it loads together? OR is there a way to put a sub-window, like modern applications have where the user can go to File->New-> and say, "load from file", and then be allowed to navigate to the .txt file on his computer?

My main goal is to be able to email this to friends and have them use it, but I googled around and only found a few people having similiar issues with not-so-clear answers. And if the JAR file cannot do this, what can? Will making it a .exe change anything?

Screenshot of Marks.txt file

I tried to make this question as legible as possible and if there is any other information I can provide you guys, please let know.

ennth
  • 1,698
  • 5
  • 31
  • 63
  • OK, I was able to atleast get the .JAR file to EXPORT and RAN on my desktop. The only option that worked in Eclipse IDE was "Copy required libraries into a subfolder next to the JAR". I *then* had to drag the Marks.txt AND .JAR file into the FOLDER. I would still like to know if its possible to add a sub-window into the GUI, that would allow the user to "LOAD" any file from their computer, like we do on any modern app/program i.e. File-> Open on MS word! – ennth May 22 '18 at 04:17
  • I am guessing that you need to include the file in your jar. If yes, just [read the file from the classpath](https://stackoverflow.com/a/1464366/1759128). – ItachiUchiha May 22 '18 at 06:51

1 Answers1

0

If you don't need to write to the file, including the file as resource in the jar would be your best option. This way you can always be sure the file is available via jar entry name. E.g.

try (InputStream is = getClass().getResourceAsStream("/data/Marks.txt");
     InputStreamReader isr = new InputStreamReader(is);
     BufferedReader br = new BufferedReader(isr)){
    model = CourseSection.loadFrom(br);
}

If you also need to write to the file, you can use FileChooser to open a dialog for choosing a file:

FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Marks File");
fileChooser.getExtensionFilters().add(new ExtensionFilter("Text Files", "*.txt"));

File file = fileChooser.showOpenDialog(null); // you could also pass a owner window here

if (file != null) {
    // TODO: handle completed file selection by user
}

The issue described in the comments indicates that there is a problem with the classpath though. You need to make sure all required classes are available when the application is run...

fabian
  • 80,457
  • 12
  • 86
  • 114
  • Thanks you, I was able to add the FileChooser to prompt the user to select the file from their computer and the JAR ran correctly i.e. [link](https://gyazo.com/fad15e35f026fc5195e0f7a9fc914433). As stated above, I was able to bypass this and keep the BufferedReader the way I initially had it,but I had to export the jar with option #3 **"Copy required libraries into a subfolder next to the JAR" inside Eclipse"**. This means that the .jar and to be inside a folder containing Marks.txt to run. This is really cool – ennth May 23 '18 at 01:41