0

I have my project structure like this

--src

--------------resources

--------------saves

I use this subfolder at multiples times like this:

 File tempFile = new File("src/saves/" + videoName + ".json");

or sometimes like this :

ImageView icon = new ImageView("resources/edit.png");

and :

File f = new File("src/resources/zelda.mp4");

For some reasons it works for the icon but not for the video/file, when I try I get this error:

Caused by: MediaException: MEDIA_UNAVAILABLE : C:\Users\histackoverflow\IdeaProjects\project2\resources\zelda.mp4 (specified path can't be found)

so I want to know what is wrong in my way of using path ?

J.erome
  • 688
  • 7
  • 26

1 Answers1

0

@Edit: this works fine for referencing files in IDE. For appropriate file references in jar, follow this: Java Jar file: use resource errors: URI is not hierarchical

I wouldn't recommend typing in the path as a String. A better way of loading files is placing them into recources (you already did that) and call the rescource by class reference and getting URI from it:

    InputStream file = new FileInputStream(getClass().getResource("/edit.png").getPath());
    Image image = new Image(file);
    ImageView icon = new ImageView(image);


    File f = new File(getClass().getResource("/zelda.mp4").toURI());

Make sure, that your directory with resources is marked as a resource directory.

Embid123
  • 467
  • 5
  • 17
  • 1
    I will suggest not to Copy paste the code, help the user who has asked the question with proper answer with relevant to his/her code. It is just a suggestion. – Sambit May 19 '19 at 17:13
  • 1
    Yes, you can. But you probably need to add a slash before `saves`, this means `/saves/xy.json` is the path. – Embid123 May 19 '19 at 17:18
  • Okay I tried, inside the IDE it works, but when I tried with the .jar I have ```Caused by: java.lang.IllegalArgumentException: URI is not hierarchical``` – J.erome May 19 '19 at 17:20
  • Right, when creating a jar you cannot use URI. Try this instead: https://stackoverflow.com/questions/10144210/java-jar-file-use-resource-errors-uri-is-not-hierarchical – Embid123 May 19 '19 at 17:22