qwpeo
qwpeo

Reputation: 11

How can I use resource files in eclipse?

I am trying to access a file in my src folder.

 skillImage = ImageIO.read(getClass().getResourceAsStream("/flame.png"));

This will produce an exception:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)

I've checked that src actually is my source folder (properties -> java build path -> Game/src is listed)

When I'm running it from a jar it works just fine.

Upvotes: 0

Views: 98

Answers (1)

howlger
howlger

Reputation: 34155

Generally, getResourceAsStream(String name) works for JAR files as well as for unpacked class folders. Please note the following:

  1. Use "flame.png" instead of "/flame.png" as resource name
  2. Make sure, flame.png is copied to the package output folder (bin/...) of the class where getClass() is called:

    • Plain Java projects: just copy the file into the package containing the class with the code getClass().getResourceAsStream("flame.png"):

enter image description here

  • Maven Java projects: for a class com.example.Foo containing the code getClass(), the default location for flame.png would be src/main/resources/com/example/:

enter image description here

Upvotes: 1

Related Questions