0

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.

howlger
  • 31,050
  • 11
  • 59
  • 99
Shane
  • 25
  • 4
  • How did you create the JAR? How are you running it? What error do you get? The syntax to run a jar is `java -jar .jar` but that JAR has to be of runnable type for this to work. – Jaywalker Aug 10 '18 at 22:02
  • Did you run the command in the java -jar command in the directory where your jar is located? – Aman J Aug 10 '18 at 22:02
  • @Jaywalker I created the Jar by exporting the program as a Runnable Jar File using Eclipse. I ran the Jar File from my desktop in both instances and I received no errors when compiling the program. I tried using java -jar .jar, but I received the following error message, 'Unable to access javafile .jar'. – Shane Aug 10 '18 at 23:06
  • Please make sure that you are having Resources folder with file Highscore.txt besides the jar and any external dependencies. If there are dependencies, you could choose to generate the jar with dependencies into a sub-folder. Refer: https://stackoverflow.com/questions/11033603/how-to-create-a-jar-with-external-libraries-included-in-eclipse – sette Aug 10 '18 at 23:10
  • @AmanJ Yes I specified the location of the Jar file as well but I still come up with the same "Unable to access javafile" error. I ran the command as java -jar "C:\Users\Shane\Desktop\.jar" – Shane Aug 10 '18 at 23:14
  • @sette When exporting the Runnable Jar file I had used the option 'Extract required libraries into generated JAR' so I followed the instructions and used the 'Package required libraries into generated JAR' option instead but the problem is still occurring. The Jar files shows that it has been opened but remains in a blank state. – Shane Aug 10 '18 at 23:17
  • @Shane I did face a similar issue with a Java SWT application. So I exported the application with the option "Copy required libraries into..". It worked fine for me. In your code, you have "Text/Highscore.txt", where actually its located when you export the jar? – sette Aug 10 '18 at 23:30
  • @sette I tried using the "Copy required libraries into" option and I folder was created but the folder is empty. As for "Where actually its located when you export the Jar?", I'm not actually too sure to be honest. Previously I read from files that I would place in the same directory as the Jar file which was easy to do, but to make the program prettier I wanted to try, I guess, 'imbed' the text file into the runnable Jar file. The Text/Highcore.txt is located within the resource folder that is a source folder for the program. – Shane Aug 10 '18 at 23:39

0 Answers0