1

I have issue with relative path to my arrow.png.

I have the following set up of directories:

--resources
  --arrow.png
  --Accounts.txt
--src
  --Main.java

And in my Main.java I am trying to load image like this:

Image image = new Image("resources/arrow.png");

However, I am getting the following error:

Invalid URL: Invalid URL or resource not found

Which is suprising, as couple of lines above it, I load another file like this:

FileInputStream fstream = new FileInputStream("resources/Accounts.txt");

And it works..

Where am I doing mistake?

4
  • what is the package of Image ? Just look at the source code to see what happens this constructor Commented Mar 25, 2016 at 9:52
  • import javafx.scene.image.Image; Commented Mar 25, 2016 at 9:53
  • did you try with Image image = new Image(getClass().getResourceAsStream("resources/arrow.png")); ? Commented Mar 25, 2016 at 9:54
  • Try using the constructor: Image(InputStream). Does the problem repeat? Commented Mar 25, 2016 at 9:57

3 Answers 3

5

Simply replace this code:

Image image = new Image("resources/arrow.png");

with this

Image img = new Image("file:resources/arrow.png");

Sign up to request clarification or add additional context in comments.

1 Comment

This is yet the simples answer. Thanks!
2

Try with:

Image image = new Image(this.getClass().getResource("resources/arrow.png"));

2 Comments

this actually throws : Error:(183, 47) java: no suitable constructor found for Image(java.net.URL) constructor javafx.scene.image.Image.Image(java.lang.String) is not applicable (argument mismatch; java.net.URL cannot be converted to java.lang.String) constructor javafx.scene.image.Image.Image(java.io.InputStream) is not applicable (argument mismatch; java.net.URL cannot be converted to java.io.InputStream)
try this Image image = new Image(getClass().getResourceAsStream("resources/arrow.png"));
0

You can also use ImageIO, personally I think it is simpler to use:

img = ImageIO.read(new File("resources/arrow.png"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.