1

I'm trying to isolate where the problem could be when trying to add an image to the class directory. (Doing this so when I export as a runnable JAR, the image is included in the package).

So I've got the strawberry.jpg file sitting in 'C:\Users\sean\workspace\myApps\src\testing' Could you advise what I'm missing? Thanks!

package testing;

import java.awt.*;
import javax.swing.*;

public class IconTest {

    public static void main(String[] arguments) {

        JFrame frame1 = new JFrame();
        frame1.setTitle("Frame1");
        frame1.setSize(500, 500);
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout flo = new FlowLayout();
        frame1.setLayout(flo);

        JLabel label1 = new JLabel(new ImageIcon(
            IconTest.class.getResource("strawberry.jpg")));
        frame1.add(label1);
        frame1.setVisible(true);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Synapse
  • 13
  • 1
  • 4

2 Answers2

1

I would use SomeClass.class.getResourceAsStream("...") as in the following example:

public static void main(String[] arguments) throws IOException {

    JFrame frame1 = new JFrame();
    frame1.setTitle("Frame1");
    frame1.setSize(500, 500);
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    FlowLayout flo = new FlowLayout();
    frame1.setLayout(flo);

    InputStream resourceAsStream = IconTest.class.getResourceAsStream("strawberry.jpg");
    Image image = ImageIO.read(resourceAsStream);

    JLabel label1 = new JLabel(new ImageIcon(image));
    frame1.add(label1);
    frame1.setVisible(true);
}
perror
  • 7,071
  • 16
  • 58
  • 85
patrick
  • 194
  • 1
  • 3
  • Thanks Patrick - it works! Though I did modify the directory as the above comment to Kennet. – Synapse Mar 31 '14 at 03:20
  • Hi Patrick. Though the code works in Eclipse, exporting to a Runnable Jar file and executing produces no results - no window and graphic. The same result is seen for another application using the principle you advised above. any ideas? Thanks! – Synapse Mar 31 '14 at 07:44
  • I know the question was asked forever ago but when I have done this in the past I always included the file such as "stawberry.jpg" in the same directory as the "IconTest.java". I did this so that it would be included in the jar. – patrick Nov 06 '14 at 22:18
1

Put the image file in the directory where your compiled classes are located and change the path in yor code by adding "/" before the filename:

JLabel label1 = new JLabel(new ImageIcon(
        IconTest.class.getResource("/strawberry.jpg")));

Resources are searched for in the class path.

Kennet
  • 5,736
  • 2
  • 25
  • 24
  • Thanks Kennet. I tried that, but still received "source not found" in the debug log. I tried copying the strawberry.jpg file to 'C:\Users\sean\workspace\SM Apps\bin\testing' too. – Synapse Mar 31 '14 at 03:17
  • Just managed to get the code below to work, though with the modified directory above. :-) – Synapse Mar 31 '14 at 03:19