2

I want to add an image to a Jpanel Class, I use the method getResource() but it returns Exception in thread "main" java.lang.IllegalArgumentException: input == null!, because the path into the image folder that i give is not valid, now I want how to get the valid path, I'm aware that maybe it's really easy and it's not a question that I've to ask here but I'm spending a lot of time and trying different path but it doesn't work;

here the Jpanel class

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;


public class Application extends JPanel  {

    private BufferedImage image;

    public Application() {
          try {
                image = ImageIO.read(getClass().getResource("/ressources/image.png"));
          } catch (IOException ex) {
                System.out.println("problem! image can't be loaded!");
          }
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, null);
    }
}

and here the main class

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

public class mainClass {

        public mainClass () {

            JFrame app = new JFrame();
            app=.setTitle("main window");
            app=.setSize(400, 400);
            app=.setLocationRelativeTo(null);
            app=.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            app=.add(new Application()); 
            app=.setVisible(true);
        }

        public static void main(String[] args) {
            mainClass class = new mainClass();
        }
    }

the error is clearly in the line image = ImageIO.read(getClass().getResource("/ressources/image.png"));

cause the path is not valid, I use a maven project, so the path of this classes is : projectName\src\main\java\application\mainClass.java projectName\src\main\java\application\Application.java

I created a folder "ressources" and i put the image there, here the path projectName\ressources\image.png

thank you.

Bhushan Uniyal
  • 5,575
  • 2
  • 22
  • 45
cbInfo009
  • 71
  • 1
  • 4
  • 14
  • Based on your directory structure, I assume you're using Maven. Resources you want added to the jar (which can then later be accessed via `getResources`) should be placed in the `projectName\src\main\resources`, they are then accessible by omitting the `resources` directory name. For example, if the you had a resource named `projectName\src\main\resources\Image.png`, you would then use use `getClass().getResource("/Image.png");` – MadProgrammer Dec 05 '15 at 01:52
  • @MadProgrammer now i got it, thanks to you. – cbInfo009 Dec 05 '15 at 04:52
  • Do you mean "resources"? Is the typo "ressources" causing the problem? – Christopher Bottoms Dec 05 '15 at 06:34
  • @Chrustopher Bottoms actually like MadProgrammer explained, the folder resources should be in the src/main folder, and yes I wrote ressources (in french lol) and it should be resources. :) – cbInfo009 Dec 05 '15 at 20:02

2 Answers2

4

You need to place your image in src/main/resources/image.png for it to be seen as in the resource root of this project...and watch that spelling of ressource. See http://www.mkyong.com/java/java-read-a-file-from-resources-folder/ for a nice summary of resource loading.

Then this line should work;

image = ImageIO.read(getClass().getClassLoader().getResource("image.png")

4

Answer of both Aidan and abu are correct, but there is something we could clarify here.

The standard root folder for a project is defined as "src" ( JavaProject/src ), so we just could put the image.png in this folder and type:

1st method: image = ImageIO.read(new File("image.png"));

from the point of view of your program (in Eclipse) this yould give the same result as this:

2nd method: image = ImageIO.read(getClass().getClassLoader().getResource("image.png"));

Now, the difference is that, when you are going to export your project in a .jar file, the first line will not export the image.png file, because the export only compiles resources at first and you will get errors because the programm doesn't see the image anymore. This is where the 2nd method is useful. Because it exports also images inside your jar file. You can watch with winrar inside to verify it.

Andy McRae
  • 525
  • 7
  • 15