Search code examples
javanetbeans2d-games

Why will NetBeans 8.1 run a project right in the editor, but when I compile it into a jar, I get errors


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]);
            }
        }
    }

Solution

  • 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.