Exporting a Final Project

I just recently finished a game using pure Java (no libraries like libgdx or lwjgl). When I exported it from Eclipse (Right-Click Project > Export… > Java > Runnable JAR File > Extract / Package Required Libraries Into Generated JAR > Finish) when opened, the JAR file showed a blank (grey) JFrame without any graphics drawn onto it. Could this be a problem with my code, (even though it runs fine in Eclipse)? Or possibly the way that I am exporting it? I am also using a sprite sheet for a pixel font in the game, but the background and characters are created in the game, so that most likely isn’t the problem.

your images folder need to be next to the jar file.

Thanks for the help! It worked, but how can I prevent players from modifying the sprite sheets?

Sorry i dont know any method to do that :confused:

If the images don’t need to be edited then you can put them in your JAR instead.

I usually use NetBeans but I have Eclipse as well so I’ll try to guide you through doing this. First make a folder in the root of your project where you want to store your images, if you don’t already have one, and put the images in it. I’d use ‘res’ or ‘img’ as the name to make your intent clear.

Next open Eclipse and right click on the project, go to ‘Java Build Path’ and click on ‘Add Folder’, find and select the folder you used in the previous step. You should now see this next to ‘src’ in Eclipse. Anything in here will now be put in your JAR.

You will probably need to change the code to load these images. Something like:

this.getClass().getResourceAsStream("/<Image_Name>");

Should do this for you, this gets you an InputStream. Note the slash, you will need it. Or maybe:

this.getClass().getResource("/<Image_Name>");

Which gets a URL.

Hope this helps!

Thanks for your help! I have the build path set up, but I still don’t understand the way that you suggested loading the images. Currently, I am using Buffered Images and storing the data in an array of pixels:


private BufferedImage image;
image = ImageIO.read(new File("res/spritesheets/font.png"));

private int[] pixels;
pixels = image.getRGB(0, 0, width, height, pixels, 0, width);

Simple, replace:

ImageIO.read(new File("res/spritesheets/font.png"));

With:

ImageIO.read(this.getClass().getResourceAsStream("/<Image_Name>"));

Putting “/font.png” in there as the name should work. :wink:

Please allow my pre-emptive strike :slight_smile:

Fair point. Last time I did this I just used the default package. In this case assuming ‘res’ is the package folder and spritesheets the package “/spritesheets/font.png” would appear to be what is actually needed. Or at least it was in my dummy. Nice utility by the way. I’m sure that’ll come in handy… :smiley:

Thank you everyone for the help. After this addition, there is a slight 2 second lag during startup, but the game seems to be running fine.