mkjrfan
mkjrfan

Reputation: 87

Exported jar file freezes after executing

Hi I've been having problems trying to export my java code into a runnable jar file.

When I double click the .jar file it starts up but for some reason the only image that I use doesn't show up (since this was a basic test all the objects are draw using shapes from what I think is java.awt). So the only thing that doesn't show up is a bullet sprite in which I put in a data folder next to the src folder.

The main problem is that it seems to crash or freeze after a few seconds of running the game. (I'm was using eclipse to run it and it worked fine).

Now I'm not that experienced with making this without the compiler so I'm not sure if I had missed anything when making this program

Here is what I have to get the image (which I believe is the main problem):

private BufferedImage pShot;

//then in the public World method

try {
        pShot = ImageIO.read(new File("data/bulletShot2.png"));

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Upvotes: 0

Views: 1542

Answers (1)

epoch
epoch

Reputation: 16615

+ src
    + resources
        - bullet.gif 
    + com
        - blah

Read your image file like this:

InputStream is = Thread.currentThread().getContextClassLoader()
        .getResourceAsStream("resources/bullet.gif");

You were most likely refering to your file like this:

File bullet = new File("./bullet.gif")

Which won't work

Upvotes: 4

Related Questions