1

I have a form with that code:

public Form() 
{
    initComponents();
    try 
    {
        File file= new File("avatar.jpg");
        BufferedImage image= ImageIO.read(file);
    } 
    catch (IOException ex) 
    {
        System.out.println("Failed to load image");
    }
}

The problem is that the code always throws the IOException and enters in the catch block.
So the file isn't read.
I have created the project with Netbeans 7.2, and the directory looks like this:

Directory

What's the problem? Maybe the file shouldn't be there but in the father directory? Or what?

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187

4 Answers4

1

It looks like you have a namespace of poker.* It all depends on where the jvm is initialized from. Where is your main? Is it in /Users/ramy/NetBeansProjects/Poker/src?

Also, I suggest you use getResource() for all of your file loading needs, especially inside jars.

this.getClass().getResource("/resource/buttons1.png")
or
this.getClass().getResourceAsStream("/resource/TX_Jello2.ttf")

You can find out where your programs default path is by doing the following:

System.getProperty("user.dir");
C Fairweather
  • 696
  • 4
  • 20
1

Is your image being packaged within your jar? to find this out, extract you jar file like you would an ordinary zip file and check if the image is anywhere there (normally located by jarname\packagename\filename. If so then you'll need to extract your image as a resource using getResourceAsStream().

It would be something like:

public class Test {
  private static final String absName = "/yourpackage/yourimage.jpg";

  public static void main(String[] args) {
    Class c=null;
    try {
      c = Class.forName("yourpackage.Test");//pkg is the package name in which the resource lies
    } catch (Exception ex) {
      // This should not happen.
    }
    InputStream s = c.getResourceAsStream(absName);
    // do something with it.
  }

    public InputStream getResourceAsStream(String name) {
      name = resolveName(name);
      ClassLoader cl = getClassLoader();
      if (cl==null) {
        return ClassLoader.getSystemResourceAsStream(name); // A system class.
      }
      return cl.getResourceAsStream(name);
    }

    public java.net.URL getResource(String name) {
      name = resolveName(name);
      ClassLoader cl = getClassLoader();
      if (cl==null) {
        return ClassLoader.getSystemResource(name);  // A system class.
      }
      return cl.getResource(name);
    }

    private String resolveName(String name) {
      if (name == null) {
        return name;
      }
      if (!name.startsWith("/")) {
        Class c = this;
        while (c.isArray()) {
          c = c.getComponentType();
        }
        String baseName = c.getName();
        int index = baseName.lastIndexOf('.');
        if (index != -1) {
          name = baseName.substring(0, index).replace('.', '/') + "/" + name;
        }
      } else {
        name = name.substring(1);
      }
      return name;
    }
}

Reference:

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
0

Without seeing the error I would say the most likely cause is it can't find the file. So I suggest you replace "avatar.jpg" in the File constructor with the absolute file path to it. e.g.

File file = new File("INSERT_PATH_TO_FILE/avatar.jpg");
Chris B
  • 925
  • 4
  • 14
0

You cannot assume the image will be "there" because the relative path between your .java and the image seems ok.

Accessing a resource depends of your "kind" of project (Web, standalone....). In your case, you can try to get the image from your classpath

final File inputFile = new ClassPathResource("....").getFile();
    final BufferedImage inputImg = ImageIO.read(inputFile);
Samuel EUSTACHI
  • 3,116
  • 19
  • 24