Aimad Majdou
Aimad Majdou

Reputation: 583

load an image from project's folder in java

I want to load an image which is in my projet folder as : /src/images/URL.jpg

I tried this code :

BufferedImage image = ImageIO.read(getClass().getResource("/images/URL.jpg"));

But I'm getting this error :

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(ImageIO.java:1388)
    at Personel.PersonnelMainForm.print(PersonnelMainForm.java:464)

How can I solve this problem ?

Upvotes: 2

Views: 21870

Answers (3)

albgorski
albgorski

Reputation: 117

I suppose you have a java class in the package. You have to move up so many times as package levels. Example: Java class is defined as org.test.MyClass you have to go up twice (../../) to be in the main directory.

Upvotes: 0

Humungus
Humungus

Reputation: 585

You can try this version of read, which takes File as an argument.

BufferedImage image = ImageIO.read(new File("path"));

where path is the path to you file, absolute or relative as you need.

Another option, if you really want to load it as a resource, would be editing your classpath, as per this question.

Upvotes: 2

Andrew Cumming
Andrew Cumming

Reputation: 965

From personal experience I use:

BufferedImage image = ImageIO.read(getClass().getResourceAsStream("/images/image.jpg"));

I get the resource as a stream and that seems to work fine for me.

Upvotes: 4

Related Questions