You're image path is somewhat correct...
But...
When using getClassLoader()
, you don't use the extra /
in front of images
.
getClass().getClassLoader().getResourceAsStream("images/bg.jpg");
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Main {
BufferedImage img;
public static void main(String[] args) {
Main main = new Main();
main.load();
}
public void load() {
try {
ClassLoader cl = this.getClass().getClassLoader();
System.out.println("CL:" + cl);
InputStream url = getClass().getClassLoader().getResourceAsStream("resources/stackoverflow5.png");
System.out.println("URL:" + url);
this.img = ImageIO.read(url); // Null argument exception
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(img)), "No ClassLoader", JOptionPane.PLAIN_MESSAGE);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
If you _don't use the getClassLoader()
, then you do need it
getClass().getResourceAsStream("/images/bg.jpg");
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Main {
BufferedImage img;
public static void main(String[] args) {
Main main = new Main();
main.load();
}
public void load() {
try {
ClassLoader cl = this.getClass().getClassLoader();
System.out.println("CL:" + cl);
InputStream url = getClass().getClass().getResourceAsStream("/resources/stackoverflow5.png");
System.out.println("URL:" + url);
this.img = ImageIO.read(url); // Null argument exception
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(img)), "With ClassLoader", JOptionPane.PLAIN_MESSAGE);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Why?...
As simply as can possibly be state here
- When you use
.getClass().getResource(fileName)
it considers the location of the fileName is the same location of the of the calling class. - When you use
.getClass().getClassLoader().getResource(fileName)
it considers the location of the fileName from the root
Note: My file structure is similar to your
ProjectRoot
resources
stackoverflow5.png
mypackage
Main.java