Results 1 to 9 of 9

Thread: [RESOLVED] Images 'embedded' into JAR file

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Resolved [RESOLVED] Images 'embedded' into JAR file

    Hi,

    We are building a game that uses some sprites (images) which are simple GIF files. We used to have those files in a separate folder called 'sprites' and loaded them like this:
    Code:
    ImageIcon icon = new ImageIcon("sprites/image1.gif");
    This worked fine, but it requires the same folder of sprites after distributing, leaving the sprites 'vulnerable' to editing or even removing by the user. We don't want that.

    So, I decided to place all the sprites into the same folder as the java source files. If I understand correctly, they should then automatically be 'embedded' into the JAR file, right?

    Then, to load an image, I use the following code:
    Code:
    URL url = MainMenu.class.getResource("image1.gif");
    Image img = Toolkit.getDefaultToolkit().getImage(url);
    Here, 'MainMenu' is the main class, I'm not sure if I have to use this in ALL classes (as the code is also present in other classes).


    Anyway, this seems to work just fine when debugging or running from within the IDE (NetBeans). As soon as we compile and try to run the JAR executable however, it fails.

    The game starts up fine, shows a few of the images, but not all of them. As far as I can see, our method of loading a level fails somehow. But I changed NOTHING in that method at all. The only thing I changed is how to sprites are drawn (I showed how above), and I'm sure that is where the problem lies.
    I use the code as above in the drawing method to draw sprites on to the level, but I use it in other methods as well, to draw a logo onto a panel.

    As said, from the IDE, everything works. But when running the compiled JAR file, only some things work. The logo works, but the sprites don't. And I'm using the same drawing method....


    Does anyone have any idea what could cause this, especially why it works from within the IDE but not from outside?


    Thanks!

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Images 'embedded' into JAR file

    "MainMenu.class.getResource("image1.gif");" tries to locate the image.gif in the same folder as MainMenu.class. If it's in the same folder (package) it should work just fine, otherwise set the relative path to image.gif. e.g. "../resources/image1.gif"
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Images 'embedded' into JAR file

    It is in the same folder. I just chucked all sprites into the same folder as all .java source files. At first I tried putting them in a different folder (and they ended up in a different package, which did look much neater) but I kept getting some kind of ClassDef[something] not found, so I decided to just put them in the same place. It's a bit of a chaos in the IDE but yeah...

    The 'funny' thing is that it does work perfectly fine when running from inside the IDE. So if I was referencing the wrong image path (this has happened before indeed) it would give me some URL error in a load image function (sorry, I can't remember the exact error messages; the important thing is: it didn't run). Since it runs from within the IDE I think we can be reasonably sure that the images do exist and they can indeed be loaded.


    I did find out something else though. There's some images that do work, and others that don't. This is what bugs me most. I can't see what's different between those images... They are all GIFs, and even though some of them are transparent while some aren't (which I thought was the issue at first), some transparent images cause the problem while others don't (and vice versa).

    I have one last resort I guess: re-create the images that cause a problem and see if that somehow helps... I have little faith though...

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Images 'embedded' into JAR file

    The IDE (Netbeans) when running uses the compiled class files not the compressed jar. And how exactly does it fail?
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Images 'embedded' into JAR file

    It just hangs. I can't do anything anymore, not even close it. I have to use the task manager and kill the process named 'javaw' and then it closes. Then it takes like 10 minutes before I can start it again. If I try sooner it just keeps opening 'javaw' processes but no GUI ever comes up.

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Images 'embedded' into JAR file

    Wow this seems like a really weird behavior. So how do you know it's about the images. Couldn't it be another section of your code?
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Images 'embedded' into JAR file

    I seem to have found the problem, although I still don't understand.

    For some reason, people were using wrong capitalization in the sprite file names (for example "Wall.gif" instead of the actual file "wall.gif"). After fixing the capitalization, it seems to work.

    What I don't understand is how it can work in the IDE then... Does that do something to the file names to ignore capitalization while the jar file doens't? Can't imagine that...

  8. #8
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Images 'embedded' into JAR file

    Urls are case sensitive but in Windows file paths are NOT
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  9. #9
    New Member
    Join Date
    Dec 2009
    Posts
    1

    Exclamation Re: Images 'embedded' into JAR file

    Quote Originally Posted by ComputerJy View Post
    Urls are case sensitive but in Windows file paths are NOT
    Actually, in the underlying kernel (and I think in the WINAPI also) subsystem they are.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width