0

I am trying to make a background for a java game. The code:

bg = new ImageIcon("src/sprites/bg.png").getImage();

works fine when I am in development, but once packaged into a jar, it doesn't seem to work. Also, if it helps, I am using eclipse.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

2 Answers2

3

To create an ImageIcon from an image file within the same jar your code is loaded:

bg = new ImageIcon(getClass().getResource("/sprites/bg.png")).getImage();

Then put your image file in your classes directory instead of your "src" directory (e.g. "classes/sprites" in your case).

(duplicate of Java Swing: Displaying images from within a Jar)

Community
  • 1
  • 1
xav
  • 5,452
  • 7
  • 48
  • 57
  • Well, how I have it set up I need to set it up like its a variable. When I have the variable, I use this code (g.drawImage(Main.f.i.bg, 0, 0, 1920, 1080, null);) to draw the image. Do you have any other alternitaves to that I could use? – user2923619 Mar 19 '14 at 23:40
  • 2
    Without knowing the relationship between the class and the images, you might consider using `/sprites/bg.png` instead, as `sprites/bg.png` implies that the `spriates` directory is in the same package as the class calling `getClass`... – MadProgrammer Mar 19 '14 at 23:43
  • @MadProgrammer: Right! Just fixed my answer. – xav Mar 19 '14 at 23:47
  • This does work in the development, but now instead of not appearing in the exported jar, the jar itself wont even turn on! Also, specify what you mean by the classes directory. I think I know what you mean, but Im not fully sure. ----- EDIT: It turns out I have to add a slash when exporting, but when it isnt exported the slash crashes it. – user2923619 Mar 20 '14 at 00:00
  • By _"classes directory"_ I meant the directory where ".class" files are located. What do you mean by _"jar itself wont even turn on"_? The application does not start anymore by executing the JAR? Maybe you're facing a `NullPointerException` due to invalid image resource path. – xav Mar 20 '14 at 00:04
  • I got it. In Eclipse, you may want to put your images in a "res" directory for example, then add this directory as a "Source folder" (using a right-click on this directory in Eclipse), then include all files "`**/*.png`" using right-click, "Configure build-path", in "Included" item. Then Eclipse will put your images in your classes directory. – xav Mar 20 '14 at 00:21
0

To read it any resource from classpath, you need to provide the path relative to JAR starting with "/". Example, let's you have jar "test.jar" and inside that you have any class in com.test.package.Testing.class so to get this class as resource you need to use path

"/com/test/package.Testing.class"

You can always get a resource as stream which is loaded in JVM. For example you have bundled your image file in your jar which is in your classpath.

First get the input stream of image file as resource by using

Inputstream is = AnyClassWhichIsInSamejar.class.getClass().getResourceAsStream("/sprites/bg.png");

or

getClass().getResourceAsStream("/sprites/bg.png");

Now convert this input stream in your ImageIcon object

aviundefined
  • 802
  • 2
  • 10
  • 25