CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2017
    Posts
    1

    Cannot load ImageIcon image files from JAR file

    I can not load the images for my card game. My card class extends ImageIcon and I specify the path of the card image in the super method. When I compile and run it in TextPad it works, but when I export it into a JAR file I get null pointer exception.

    My code is like this:

    Code:
    super(Card.class.getResource("images/" + imagePath));
    I have tried with:

    Code:
    super("images/" + imagePath);
    
    super(Card.class.getClassLoader().getResource("images/" + imagePath));
    This is a screenshot from the decompiler of the jar file:

    http://i.imgur.com/6pMeLlN.jpg

    Any advice?

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Cannot load ImageIcon image files from JAR file

    Norm

  3. #3
    Join Date
    Aug 2017
    Posts
    36

    Re: Cannot load ImageIcon image files from JAR file

    Guys, the cases of the disk filenames and the names in the String array match exactly. If I delete Card.class.getResource the game works fine.

    If the code is like this

    super("images/" + imagePath);

    And if I make the the JAR file with this statement I need to have the images in the same folder with the JAR file and it works. But I don't want to have the images together with the JAR, I want the images to be loaded from the JAR file.

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Cannot load ImageIcon image files from JAR file

    To access a file in the jar file, treat it as a resource: use one of the getResource methods.
    Norm

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

Featured