0

I am pretty new to Java. I have made plenty of programs that run in the safety of Eclipse, but I am having trouble getting a program that I made to function correctly outside of an IDE in JAR form.

Before I get to the real questions, I'll provide some background. I wrote a program that searches a .txt file roster for a resident in an apartment, an upon selecting the resident, the program allows the user to add the residents to a mail list and for that mail list to be generated into an Excel (.xls) file via FileOutputStream and the Apache POI HSSF API. The program is complete with a Swing GUI implemented in NetBeans. In Eclipse, I have my roster, Roster.txt inside my project folder in the directory with my src and libraries. In order to read the .txt file, I use the Scanner class i.e. Scanner scan = new Scanner(new File("Roster.txt"));.

All of this works perfectly in Eclipse: I can add/remove residents from the mail list and generate .xls files into my project folder. However, once I try and export the project as a JAR, all functionality disappears. I select File->Export->Runnable JAR File, and the JAR pops up on my desktop just like it's supposed to. When I click the file, the GUI loads up perfectly, and all of the Swing components seem to be working just fine; but when I search a name, no results show up. This leads me to believe that the Roster.txt file is not being correctly read into my sorting class after the JAR is created.

Is there any part of creating a JAR that messes standard Java I/O? Should I be using args[]? How would implementing that differ from just using the scanner class to make a new (new File("Roster.txt"))?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Mattmv
  • 9
  • 1
  • when creating File with relative path, it's assuming the file is relative to the current directory. If I understand correctly, you put Roster.txt in the project root, which is the default current directory in eclipse. When you export the runnable jar, does it put it in the project root directory, or some other location. I would expect it to work if you ran your jar in same directory as Roster.txt. – DBug Nov 11 '15 at 18:34
  • 1
    duplicates: http://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar http://stackoverflow.com/questions/403256/how-do-i-read-a-resource-file-from-a-java-jar-file – Emerson Cod Nov 11 '15 at 18:36

0 Answers0