0

I am working on a game and was going to create a jar file of it to send out a beta version to some of my friends so that I would know how the game looks on different resolutions and how it runs on worse and better computers. The problem is that when I run the jar file it will be completely black and nothing will happen.

I think that I have done the path for the images wrong because if i remove all the images from the net beans folder it will look exactly like when I start the jar. I have done the path for the pictures by just writing their name and having them in the same folder as the classes.

The jar file looks like this: Two folders, META-INF and Apocalypse. Inside META-INF is the manifest file, it looks like this:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.7.0_17-b02 (Oracle Corporation)
Class-Path: 
X-COMMENT: Main-Class will be added automatically by build
Main-Class: Apocalypse.Main

Inside the Apocalypse file is all the classes and all of the images, to give an example of how I get the images in the code I get them like this ImageIcon ii = new ImageIcon("Image.png") I don't have any path to the image but shouldn't this code work if I have the images in the same folder as the classes? How else would do the path to the images (if it is them that is the problem)?

  • I would start by googling "_add image to jar file_" followed by the name of the IDE you're using. Then I would choose one of the dozen links on this site that you see in the results... This question gets asked every day. – jahroy Jun 03 '13 at 19:07

1 Answers1

2

You say:

Inside the Apocalypse file is all the classes and all of the images

But then the example code you paste is:

ImageIcon ii = new ImageIcon("Image.png")

which tries and loads from a file on your local machine. It is therefore environment dependent and not suitable for a generalized deployment.

This is not what you should do. You should include these images in the jar file and load them using the ImageIcon constructor using a URL as a parameter.

Now, in order to obtain the URL of an image when it is in the classpath (and that includes jar lookup), see Class#getResouce().

Typical usage scenario: create a utility class in your jar which will do, say:

public final class MyImageLoader
{
    // Utility class: no instantiation
    private MyImageLoader() {}

    public static ImageIcon getImage(final String resourcePath)
    {
        final URL url = MyImageLoader.class.getResource(resourcePath);
        return new ImageIcon(url);
    }
}

and then use:

final ImageIcon i1 = MyImageLoader.getImage("/path/to/image.png");
fge
  • 119,121
  • 33
  • 254
  • 329
  • This works but I also have save files and because I use the File class to create these save files I can't use a URL, so how would I make it work in this instance? – Java Programmer Jun 03 '13 at 20:01
  • For save files, you'd have to take the approach of saving somewhere behind `System.getProperty("user.home")`. However, there seems to be a catch: see [here](http://stackoverflow.com/questions/1503284/java-system-getpropertyuser-home-returns). In the case where you cannot even determine that, however, I think it'd be a sane practice to just abort the game and warn the user that "sorry, I could not determine where to save files". – fge Jun 03 '13 at 20:56