I have a problem i have never had before and couldnt find a solution on the web. I have a small Programm which uses some images to print a menu.
This is the Class i use to print the Images:
public class ViewImage extends JPanel {
private static final long serialVersionUID = 1L;
protected Image image = null;
public ViewImage(int x, int y, String path) {
this(x, y, new ImageIcon(path).getImage());
}
public ViewImage(int x, int y, Image image) {
this.image = image;
this.setBounds(x, y, image.getWidth(null), image.getHeight(null));
}
public void paintComponent(Graphics g) {
int x = ((this.getWidth() - image.getWidth(null)) / 2);
int y = ((this.getHeight() - image.getHeight(null)) / 2);
g.drawImage(image, x, y, null);
}
public void setImage(String path) {
this.image = new ImageIcon(path).getImage();
}
}
My Images are all part of the classpath an i call them with:
this.getClass().getClassLoader().getResource("MyImage.png").getPath()
Works fine until i package my programm into a jar file and run it from console with:
java -jar MyJar.jar
My programm starts well but no image are printed. No Exceptions, no Errors, nothing.
What can cause such an behaviour?
getResourceAsStreaminstead of trying to get to an actual file?