0

I have searched for a while now for a fix to this. I have a project folder in my workspace (that folder is basically my root), and in it there is a file called icon.gif. In my program, I have the following:

package com.mgflow58.Main;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class Game {

    public static void main(String[] args) {
        JFrame window = new JFrame("Guppy's Adventure");
        window.add(new GamePanel());
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setResizable(false);
        window.pack();
        window.setLocationRelativeTo(null);
        window.setVisible(true);
        window.setIconImage(new ImageIcon("icon.gif").getImage());
    }

}

The icon works fine in eclipse when I run it, but the exported jar file does not display the icon as it does in eclipse. Any idea why? I'm going crazy trying to figure this out.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
octopus58
  • 15
  • 3
  • `ImageIcon(String)` is trying to looking for the `icon.gif` within the current working directory (where the program was executed from). Where does the file reside within the context of the Eclipse project? – MadProgrammer Dec 05 '14 at 00:39
  • As I said the icon.gif file is in the root folder, outside the source folder. Just sitting in the folder containing everything for the game – octopus58 Dec 05 '14 at 00:42
  • Then these files need to be copied to the same location that the program is executed from – MadProgrammer Dec 05 '14 at 00:49
  • The program is executed from a jar file. The jar file contains all the image files needed in their proper folders. It just doesn't load for some reason, which is why I'm asking the question – octopus58 Dec 05 '14 at 00:55

1 Answers1

1

The jar file contains all the image files needed in their proper folders.

There is the problem...ImageIcon(String) is looking for the named file on the disk as described by the JavaDocs...

public ImageIcon(String filename)

Creates an ImageIcon from the specified file. The image will be preloaded by using MediaTracker to monitor the loading state of the image. The specified String can be a file name or a file path. When specifying a path, use the Internet-standard forward-slash ("/") as a separator. (The string is converted to an URL, so the forward-slash works on all systems.) For example, specify:

new ImageIcon("images/myImage.gif")

The description is initialized to the filename string.

Parameters:
filename - a String specifying a filename or path

The resources no longer reside on the disk, but are not embedded within the Jar file, so they can no longer be accessed by using File based methods.

Instead, you will need to use something like Class#getResource, for example...

window.setIconImage(new ImageIcon(Game.class.getResource("icon.gif")).getImage());

Having said this, I would also recommend using ImageIO over ImageIcon for a number of reasons, but mostly, because it will actually throw an exception if the resource can't be loaded for some reason, rather than failing silently...

Take a look at Reading/Loading an Image for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • That didn't work, I keep getting the following exception at the line i copied from your response: Exception in thread "main" java.lang.NullPointerException – octopus58 Dec 05 '14 at 12:57
  • The oracle option also didn't work, it just worked in eclipse again but not in the actual runnable jar file – octopus58 Dec 05 '14 at 17:08
  • The reason you're likely getting a null pointer exception is because the image doesn't exist in the root location of the jar, you need to provide the path for it relative to src... – MadProgrammer Dec 05 '14 at 20:46
  • How would I do that? My file is in the project folder outside src and resources. – octopus58 Dec 05 '14 at 21:29
  • The how do you know they are included in the jar file? – MadProgrammer Dec 05 '14 at 21:30
  • I opened the jar in win rar to check, the file is in the jar. – octopus58 Dec 05 '14 at 21:38
  • And what's to the path, from the root of jar, to the resources...? – MadProgrammer Dec 05 '14 at 22:15
  • I don't know exactly what you are asking, but the resources are in: GuppyAdventure/Resources/... However icon.gif is in GuppyAdventure – octopus58 Dec 05 '14 at 22:35
  • And `GuppyAdventure` is the name of the Jar or is it the top level package? – MadProgrammer Dec 06 '14 at 00:25
  • The folder that contains everything, in workspace. Not the name of the jar. The jar name is Beta026.jar – octopus58 Dec 06 '14 at 00:29
  • So in theory, `Game.class.getResource("icon.gif")` should return a reference to `icon.gif`, you could try `Game.class.getResource("/icon.gif")`. As for the other resources, you'd need to use the `Resources/` path instead... – MadProgrammer Dec 06 '14 at 00:31
  • This is hard to believe... I have done that so much times yet it didn't work... I must have had a very dumb error somewhere. Thank you so much, I'm so relieved! – octopus58 Dec 06 '14 at 02:45