1

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();
}

}

enter image description here

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
dazbrad
  • 182
  • 1
  • 12
  • 1
    ImageIcon a = new ImageIcon("images/success.jpg"); use getResouses – Madhawa Priyashantha Oct 13 '14 at 13:21
  • after reading on stakOFlow, I coded ImageIcon a = new ImageIcon(getClass().getResource("/success.jpg")); and it didnt work, why is this? – dazbrad Oct 13 '14 at 13:24
  • 1
    possible [duplicate](http://stackoverflow.com/a/25636097/2587435) – Paul Samsotha Oct 13 '14 at 13:33
  • @peeskillet yeah i saw that finally .and yeah it's not work – Madhawa Priyashantha Oct 13 '14 at 13:35
  • Instead of `"images/success.jpg"` use `"/images/success.jpg"` – Andrew Thompson Oct 13 '14 at 13:40
  • @AndrewThompson - just tried adding the "/", but that now doesnt show the image when run in eclipse or when compiled. – dazbrad Oct 13 '14 at 13:45
  • @getlost `..new ImageIcon("images/success.jpg");` That **won't** work for an embedded resource because the JVM is trying to load a file of that name. – Andrew Thompson Oct 13 '14 at 13:46
  • @peeskillet - this is defo a duplicate question. I just havent quite grasped what others have tried to describe. However, ive not seen thispage you linked to and it seems to explain in some depth. Ima go and gibe it a read a moment. Thanks so far guys. – dazbrad Oct 13 '14 at 13:47
  • 1
    *"just tried adding the "/","* I only just noticed the `images` directory does not seem to be in a place such that the content is included in the Jar. Have you checked the content of the Jar? – Andrew Thompson Oct 13 '14 at 13:48
  • BTW `p.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Oct 13 '14 at 13:49
  • The images folder doesnt get compiled into the jar at all – dazbrad Oct 13 '14 at 13:51
  • 1
    And just to be clear, I meant using it like this.. `..new ImageIcon(getClass().getResource("/images/success.jpg"));` – Andrew Thompson Oct 13 '14 at 13:51
  • @AndrewThompson, I tried all variations including what you meant, but no, thats not worked either. – dazbrad Oct 13 '14 at 13:58

2 Answers2

4

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

enter image description here

The only default directory built into the classpath (/jar) is the src. We need to either put the resources into the src

enter image description here

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 path

enter image description here


CODE we would use:

  • First Image: Can't, it won't work (this your current predicament)

  • Second Image:

    ImageIcon icon = new ImageIcon(
             getClass().getResource("/resources/stackoverflow.png"));
    
  • Third Image:

    ImageIcon icon = new ImageIcon(
             getClass().getResource("/stackoverflow.png"));
    

To configure the build path to use the third option, follow the instructions in Example 2 in this answer

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Brilliant guide, I noticed that this seems to be quite a common issue, hopefully this will clear it up for many. I took the second option as it seems much simpler to achieve, but I will also have a blast at option3 anyhow. It seems logical, but what you describe above will also apply to all resources, is this correct? Thanks anyway, works a charm. – dazbrad Oct 13 '14 at 14:33
  • Pretty much. Any resource you need to access from the classpath – Paul Samsotha Oct 13 '14 at 14:41
2

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:

ImageIcon a = new ImageIcon(getClass().getResource("/images/success.jpg")); 
Stefan
  • 12,108
  • 5
  • 47
  • 66
  • this hasnt worked either, but thanks for your effort. – dazbrad Oct 13 '14 at 13:56
  • Both answers are right. Though here a slight mistake, it should be `getResource("/images/success.jpg")` if place in `src/images`. Another point: the file names must be **case-sensitive** (zip format), where Windows file system (eclipse) is more relient. – Joop Eggen Oct 13 '14 at 14:13