I recently finished working on a project and I went to create a runnable JAR file out of it, but the problem is, as a runnable JAR it never opens. When I run the program from Eclipse it opens fast and without any errors. When I open the program as a runnable JAR file it shows that the JAR file is open, however, a display is not shown and I cannot click on the file at all.
I saw a forum post where a user was told to type java -jar <FILE NAME>.jar
and they would be able to figure out why their program wasn't opening from the output issued by the command. I actually tried this to see if it would work for me as well, but I get an error Unable to access jarfile <FILE NAME>.jar
. I was running this on Windows so I quickly sent the entire folder to my Mac laptop and booted Eclipse and the same thing happened. I was able to run the program from Eclipse with no issues, but outside Eclipse I never got a display.
The program that I am creating is for AP Computer Science students for when I am their TA in the upcoming school year, so I really need it to work. The only issue that I could see is making the JAR file unable to launch is when I read information from a text file but even that should not be an issue. Any help would be appreciated. If you need any code I can post it right away. Thank you in advance.
EDIT: By the way, the reason why I believe that it might be how I read information from a text file is because previously to read a file I would place it in the same location that the runnable JAR file. This, however, looks very ugly so I decided to try and incorporate the text file from within the resources folder that is apart of the Java source folder. To read in from a text file I use the following code:
public int getHighscore() {
ClassLoader classLoader = this.getClass().getClassLoader();
File file = null;
Scanner scanner = null;
int highScore = 0;
try {
file = new File(classLoader.getResource("Text/Highscore.txt").toURI());
scanner = new Scanner(file);
while(scanner.hasNextLine())
highScore = Integer.valueOf(scanner.nextLine());
} catch (URISyntaxException e1) {
e1.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return highScore;
}
EDIT #2: I tried running the JAR file from the command prompt as an administrator on my Windows computer and I also tried running the JAR file from terminal on my Mac. The interesting part is that both times the file would not run, I received the error message Unable to access jarfile <filename>.jar
, but when I tried opening a different JAR file that was in the same directory as the file that I am having problems with, it opened fine. I wanted to make sure that it wasn't a system-wide issue that I was having, and it's not. For some reason, this is the one and only Jar file that just will not open.
EDIT #3: Alright, so I deleted the code that I had to get the user's highscore (getHighScore
) and the code to write a user's highscore (setHighScore
) and I always deleted the Highscore.txt
file that I stored within my resources folder and I ran the program as a last hail-marry attempt to try and figure out why the program was not running, and it ran. So the problem was 100% how I am reading/writing to a text file. Thanks everyone for their time and effort in attempting to help me figure out my problem, means a lot.