I have been all over the internet trying to work out how to get an image icon displayed after compiling into a runnable jar. I discovered this problem way too late, I ran my program many times before in eclipse and every thing has worked, now 6 months later with project finished, I compiled my program with eclipse and no audio or images work. Reading on the net, it says about the location of images folder should be inside jar, but mine doesnt get put there?
I have played around with the images folder moving it inside the source folder, but it didn't work. I have a feeling that it might be something to do with the path of the resource...ebut thats only guessing.
I have built a simple program that has the same results... works when ran in eclipse, but not when compiled. Could somebody show me an example by modifying my code below. Thanks in advance.
SOURCE CODE:
package ImageIcon;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Gui {
public static JLabel c;
public Gui(){
JFrame f = new JFrame();
JPanel p = new JPanel();
p.setBounds(0, 0, 120, 200);
p.setBackground(Color.black);
p.setLayout(null);
JPanel bg = new JPanel(new BorderLayout());
bg.setBounds(50, 50, 15, 15);
bg.setBackground(Color.white);
ImageIcon a = new ImageIcon("images/success.jpg");
c = new JLabel(a);
f.setSize(100, 200);
f.setLayout(null);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.add(p);
p.add(bg);
bg.add(c);
}
public static void main(String[] args) {
new Gui();
}
}
As from your screenshot, the image exists in a folder called "images". Put it in a folder inside your classpath: src/images/success.jpg and call:
With your current directory setup, the
images
dir won't even get built into the jar. Try to extract it and you will most likely see that it's not in there.You can tell by the fact that it doesn't have the little package logo in the folder, as seen here with
resources
The only default directory built into the classpath (/jar) is the
src
. We need to either put theresources
into thesrc
or configure the build path to include the files that are in the
resources
. Once we do that, we will see the little package icon inside the folder icon. That's how we know the files are on the build pathCODE we would use:
First Image: Can't, it won't work (this your current predicament)
Second Image:
Third Image:
To configure the build path to use the third option, follow the instructions in Example 2 in this answer