5

created a new folder "resources" with all my images in eclipse. I added this folder to the class path.

Now i tried to access this images

URL url = new URL("imageA");

I also tried

URL url = new URL("resources/imageA");

Both doesnt't work. What is wrong?

Sincerely Christian

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
Christian
  • 51
  • 1
  • 1
  • 2
  • This solution works for me: https://stackoverflow.com/questions/4301329/java-class-getresource-returns-null/50387930#50387930 – Taras Melnyk May 17 '18 at 09:59

4 Answers4

3

If you are loading from your classpath you should do something like this

InputSteam is =  this.getClass().getClassLoader().getResourceAsStream("resources/myimage.png")
Chuk Lee
  • 3,570
  • 22
  • 19
1

You can use something like this if you are trying to load an image from a local location.

File file = new File("D:/project/resources/imageA.jpg");
Image image = ImageIO.read(file);

If you are planning to read the image from a URL, use the below code:

URL url = new URL("http://www.google.co.in/images/srpr/logo3w.png");
Image image = ImageIO.read(url);

Please have a look at the below code for better understanding:

package com.stack.overflow.works.main;

import java.awt.Image;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 * @author sarath_sivan
 */

public class ImageLoader {

    private ClassLoader classLoader = ClassLoader.getSystemClassLoader();
    private Image image = null;
    private URL url = null;

    /**
     * Loading image from a URL with the help of java.net.URL class.
     */
    public void loadFromURL() {
        image = null;
        try {
            url = new URL("http://www.google.co.in/images/srpr/logo3w.png");
            if (url != null) {
                image = ImageIO.read(url);
            }

        } catch(Exception exception) {
            exception.printStackTrace();
        }
        display(image, "Loading image from URL...");

    }

    /**
     * Loading image from your local hard-drive with getResource().
     * Make sure that your resource folder which contains the image
     * is available in your class path.
     */
    public void loadFromLocalFile() {
        image = null;
        try {
            url = classLoader.getResource("images.jpg");
            if (url != null) {
                image = ImageIO.read(url);
            }

        } catch(Exception exception) {
            exception.printStackTrace();
        }
        display(image, "Loading image from Local File...");
    }



    private void display(Image image, String message) {
        JFrame frame = new JFrame(message);
        frame.setSize(300, 300);
        JLabel label = new JLabel(new ImageIcon(image));
        frame.add(label);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        ImageLoader imageLoader = new ImageLoader();
        imageLoader.loadFromURL();
        imageLoader.loadFromLocalFile();
    }

}

Output

enter image description here

enter image description here

1218985
  • 7,531
  • 2
  • 25
  • 31
1

See here for directions. You can't load an image directly with URL (actually you can, but it is complicated see this question)

Actually you need to do something like this:

ClassLoader loader = ClassLoader.getSystemClassLoader();

if(loader != null) {
   URL url = loader.getResource(name);
}
Community
  • 1
  • 1
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194