0

I'm developing a game in Java, but I'm having trouble executing it as a JAR file. Everything works fine when running it from Eclipse, but once it's exported to .JAR, no images load. I've searched many similar questions, and this seems to be a common problem, however none of the answers has helped me.

In the Eclipse project, I have two build paths: src - which constains all the .java -, and resources - which contains a subfolder named images, which has the two images I need. The method I'm using is

public void loadContent()
{
    try
    {
        backgroundImg = ImageIO.read(getClass().getResource("/images/background.jpg"));
    }
    catch (IOException e)
    {
        e.getStackTrace();
    }   
}    

, being backgroundImg an object of BufferedImage

I've tried all combinations involving getResourceAsStream() instead of getResource(), "images..." instead of "/images..." and many other...

The resources are being added to the JAR, it's just that I can't load them. Does anybody know how to solve it?

Thanks in advance!

  • Have you actually confirmed the images are in the JAR file? – BarrySW19 Oct 20 '14 at 21:41
  • What is the output of `jar tvf yourjar.jar`? What happens when executing the code? What's the stack trace? – JB Nizet Oct 20 '14 at 21:42
  • Are you aware that the `getResource(...)` call is relative to the class's path within the jar? Is the image copied there? – Gábor Bakos Oct 20 '14 at 21:44
  • @GáborBakos not if the path starts with a `/`. – JB Nizet Oct 20 '14 at 21:45
  • You are right. I missed the slash in the post. (Here is the javadoc for the record: http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource(java.lang.String)) – Gábor Bakos Oct 20 '14 at 21:48
  • 1
    A number of the comments have alluded to the fact that your resources may not actually be where you think they are in the final jar. Have you verified that all the images are properly being packaged? The correct call to get your image from the jar should be `backgroundImg = ImageIO.read(getClass().getResourceAsStream("/images/background.jpg"));` where the folder `images` should be at the root of your classpath (as denoted by the leading `/`). Do you have a way to tell if this call is returning `null` when you run it from jar? – Ryan J Oct 20 '14 at 21:51
  • Opening the JAR with WinRAR, there are three folders: game - which is the package that contains all the .class -, images - which constains both images I need -, and META-INF – Fábio Xavier Oct 21 '14 at 00:27
  • This is the result of jar tvf myjarfile.jar C:\Users\Fábio\Desktop>jar tvf Jogo.jar 50 Mon Oct 20 19:21:44 BRST 2014 META-INF/MANIFEST.MF 387 Sun Oct 19 22:58:30 BRST 2014 .project 2110 Mon Oct 20 19:20:28 BRST 2014 game/Submarine.class 1627 Mon Oct 20 19:20:50 BRST 2014 game/Game.class 796 Mon Oct 20 19:18:52 BRST 2014 game/Window.class 517 Mon Oct 20 18:29:58 BRST 2014 game/Framework$1.class 1109 Mon Oct 20 18:29:58 BRST 2014 game/Framework$GameState.class 3302 Mon Oct 20 18:29:58 BRST 2014 game/Framework.class – Fábio Xavier Oct 21 '14 at 00:33
  • 11480 Mon Oct 20 17:40:30 BRST 2014 images/Submarino.png 76308 Mon Oct 20 17:40:30 BRST 2014 images/background.jpg 349 Mon Oct 20 18:26:44 BRST 2014 .classpath which - I believe - means all the files I need are packed in the JAR – Fábio Xavier Oct 21 '14 at 00:34
  • And this is the stack trace: C:\Users\Fábio\Desktop>java -jar Jogo.jar Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(Unknown Source) (...) – Fábio Xavier Oct 21 '14 at 00:43
  • I say start a new project. Simple one with one class, just trying to load that image. Follow [these instructions](http://stackoverflow.com/a/25636097/2587435). If it works, see where you may be going wrong with your project – Paul Samsotha Oct 21 '14 at 01:19
  • Hey everybody, I've fixed my problem, which was due to a really stupid mustake: one of the images filename began with an uppercase letter, but I was refering to it in the code all in lowercase. Apparently, Eclipse found the file even with that inconsistency, but when executing from the JAR that did not happen. Thank you all for your time! – Fábio Xavier Oct 21 '14 at 01:51

0 Answers0