-1

When I call:

Image photo = new Image(getClass().getResourceAsStream("/images/myPhoto.png"));

It seems to look in the bin folder of the class that I called that line in. Of course, there is no images folder in my bin folder and it yields an error. How do I re-direct it to the main directory of my project and into my 'images' folder? I tried adding the folder to my eclipse project and then right clicking and setting 'add to build path' but that just caused more compile errors.

I also don't want to define a strict location like:

c:/users/me/java/etc because if this JAR is ever on another person's computer, it won't work.

Thanks for the help.

Hatefiend
  • 3,416
  • 6
  • 33
  • 74
  • You could try using `currentDir = System.getProperty("user.dir");` to always get the current directory in which the programm is executed – bish Oct 30 '15 at 08:29
  • look at this link http://stackoverflow.com/questions/6845231/how-to-correctly-get-image-from-resources-folder-in-netbeans – Kumaresan Perumal Oct 30 '15 at 08:36
  • Doesn't work. `photo = new Image(System.getProperty("user.dir") + "\\images\\myPhoto.png")`. `java.lang.reflect.InvocationTargetException` – Hatefiend Oct 30 '15 at 09:09

1 Answers1

0

.getClass().getResourceAsStream(fileName) considers that the location of the image is the same as the calling class.

So try this:

this.getClass().getClassLoader().getResourceAsStream("images/myPhoto.png")

according to where is your image in the classpath.

Zakaria
  • 108
  • 1
  • 9