0

After extracting a Runnable Jar in Eclipse Version 2020-12 (4.18.0), when I run it with double click the image is not displayed:

enter image description here

However, when using java -jar meujar.java the image is displayed:

enter image description here

Image addition code:

JLabel lblImage = new JLabel();
lblImage.setBorder(BorderFactory.createLineBorder(Color.BLACK));
lblImage.setBounds(275, 30, 225, 225);
lblImage.setIcon(new ImageIcon("logo.png"));

The addition of the label to the frame was made by windows builder, following part of the code:

.addComponent(lblImage, GroupLayout.PREFERRED_SIZE, 225, GroupLayout.PREFERRED_SIZE)
.addComponent(lblImage, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))


Runcorn
  • 5,144
  • 5
  • 34
  • 52
Adriana
  • 3
  • 1
  • 1
    From the looks of it `logo.png` is an external file, which should be stored relative to the "working" directory. Instead, you should consider using an "embedded resource" instead, which will allow the code to load the image from within it's own bundle context (from the Jar file in most cases) – MadProgrammer Feb 13 '21 at 03:12
  • Objects in a jar are not files so you can't use the file name version of the ImageIcon constructor to access the image in the jar. – greg-449 Feb 13 '21 at 08:57
  • If I can't use the file name in ImageIcon, what should I use? @greg-449 – Adriana Feb 14 '21 at 14:43
  • See the answers in the duplicate – greg-449 Feb 14 '21 at 14:53

0 Answers0