0

I'm making a game in Java using NetBeans, when I run it in NetBeans I get an image showing, as this is the expected behavior. However when I compile the project into a jar file and run it, I get a white screen and stack-trace if run in the terminal. Here's the stack trace for the jar. I really don't know what to do as I've been searching for an answer to this for a while but haven't found anything. It's all with URLs to files and they're all correct

java.io.FileNotFoundException: res\Worlds\world1.world (The system cannot find the path specified)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileReader.<init>(Unknown Source)
        at dev.DualKeys.SIF.utils.Utils.loadFileAsString(Utils.java:13)
        at dev.DualKeys.SIF.worlds.World.loadWorld(World.java:47)
        at dev.DualKeys.SIF.worlds.World.<init>(World.java:18)
        at dev.DualKeys.SIF.states.GameState.<init>(GameState.java:24)
        at dev.DualKeys.SIF.Game.init(Game.java:56)
        at dev.DualKeys.SIF.Game.run(Game.java:86)
        at java.lang.Thread.run(Unknown Source)
java.lang.NumberFormatException: For input string: ""
        at java.lang.NumberFormatException.forInputString(Unknown Source)
        at java.lang.Integer.parseInt(Unknown Source)
        at java.lang.Integer.parseInt(Unknown Source)
        at dev.DualKeys.SIF.utils.Utils.parseInt(Utils.java:29)
        at dev.DualKeys.SIF.worlds.World.loadWorld(World.java:49)
        at dev.DualKeys.SIF.worlds.World.<init>(World.java:18)
        at dev.DualKeys.SIF.states.GameState.<init>(GameState.java:24)
        at dev.DualKeys.SIF.Game.init(Game.java:56)
        at dev.DualKeys.SIF.Game.run(Game.java:86)
        at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: 1
        at dev.DualKeys.SIF.worlds.World.loadWorld(World.java:50)
        at dev.DualKeys.SIF.worlds.World.<init>(World.java:18)
        at dev.DualKeys.SIF.states.GameState.<init>(GameState.java:24)
        at dev.DualKeys.SIF.Game.init(Game.java:56)
        at dev.DualKeys.SIF.Game.run(Game.java:86)
        at java.lang.Thread.run(Unknown Source)

Here is loadFileAsString from dev.DualKeys.SIF.utils.Utils

public static String loadFileAsString(String path) {
        StringBuilder builder = new StringBuilder();

        try {
            BufferedReader br = new BufferedReader(new FileReader(path));
            String line;
            while ((line = br.readLine()) != null) {
                builder.append(line + "\n");
            }

            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return builder.toString();
    }

and loadWorld from dev.DualKeys.SIF.worlds.World

private void loadWorld(String path) {
        String file = Utils.loadFileAsString(path);
        String[] tokens = file.split("\\s+");
        width = Utils.parseInt(tokens[0]);
        height = Utils.parseInt(tokens[1]);

        tiles = new int[width][height];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                tiles[x][y] = Utils.parseInt(tokens[(x + y * width) + 2]);
            }
        }
    }
NinjaNube008
  • 43
  • 1
  • 8
  • The URL to file is probably different in a packaged jar, then on your IDE. – achAmháin Jun 12 '18 at 07:50
  • @notyou Is there a way I can fix that? – NinjaNube008 Jun 12 '18 at 07:51
  • java.lang.NumberFormatException: For input string: "" ... Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: 1 these have nothing to do with compilation – Stultuske Jun 12 '18 at 07:52
  • @Stultuske the array data is loaded from a file that isn’t being loaded right – NinjaNube008 Jun 12 '18 at 07:54
  • It might work if you include a backslash at the start of your path. Or perhaps read this: https://stackoverflow.com/questions/941754/how-to-get-a-path-to-a-resource-in-a-java-jar-file – achAmháin Jun 12 '18 at 07:58
  • 1
    Please [edit] your question and show us the code you use to open/retrieve that file (essentially the method that corresponds to line 50 of `World.java`). **[edit]** your question, do **not** post code in comments. –  Jun 12 '18 at 08:12
  • @a_horse_with_no_name or rather method `loadFileAsString` of class `dev.DualKeys.SIF.utils.Utils`. – Maurice Perry Jun 12 '18 at 08:14

1 Answers1

1

I assume that the last two exceptions are a consequence of the first one: the program cannot find file res\Worlds\world1.world. As the program is using a FileInputStream, you can't include the file in your jar.

Maurice Perry
  • 9,261
  • 2
  • 12
  • 24