0

As you can see here the file is clearly available in the project in a source folder, however when running the program I am receiving an file does not exist exception. Below I have posted the full error:

com.interactivemesh.jfx.importer.ImportException: StlMeshImporter read(File file) : file doesn't exist !
at com.interactivemesh.jfx.importer.stl.StlMeshImporterImpl.read(Unknown Source)
at com.interactivemesh.jfx.importer.stl.StlMeshImporter.read(Unknown Source)
at minimalist.TriangleMeshTest.start(TriangleMeshTest.java:30)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)
  • Note the difference between absolute and relative paths. See: http://stackoverflow.com/questions/23896840/relative-to-absolute-path-in-java and http://stackoverflow.com/questions/3204955/converting-relative-paths-to-absolute-paths/3205019#3205019 – Itai Mar 20 '17 at 11:40

1 Answers1

1

The problem is that you are using an absolute path. If you want to use an absolute path, you need to provide the full path from root (/ or C:/)

If You want to use a relative path music/untitled.stl you need have the current working directory pointing to the Test folder.

One way to go about loading resources from code is to put the resources in a resource folder inside the src folder and then to use getClass().getResourceAsStream("/resource/resource_name").

So, if you were to move your music folder in the src folder you would be able to read the file like:

getClass().getResourceAsStream("/music/untitled.stl");

Note how you use a "/" here at the beginning. It works in this case because getResourceAsStream takes of care of resolving the path to your src directory using the ClassLoader.

Edd
  • 1,350
  • 11
  • 14
  • `File file = new File(this.getClass().getResource("/music/mapTest3.stl").toExternalForm()); stlImporter.read(file);` I attempted using this however, I am receiving a null pointer exception. May you provide a full code snippet below in your answer? – Politic Revolutionnaire Mar 21 '17 at 10:51
  • Did you move the music folder inside the src folder? next to the minimalist folder in your project. I tested on my end with this.getClass().getResourceAsStream("/resource/test.txt") , where resource is in my src folder, and I got a valid **Stream**. If you want to use a **File rather than a Stream**, then use getClass().getResource("/music/mapTest3.stl").getFile()); You can also take a look here for an example: https://www.mkyong.com/java/java-read-a-file-from-resources-folder/ – Edd Mar 21 '17 at 13:47
  • music is a resource folder and the way Eclipse is organized you don't have resource folders inside the src folder, rather alongside it. – Politic Revolutionnaire Mar 21 '17 at 19:06
  • Managed to get it to work by using `stlImporter.read(new File(System.getProperty("user.dir") + "/music/mapTest3.stl"));` that line, however I know that will not work when I export it to a `jar` file. – Politic Revolutionnaire Mar 21 '17 at 19:13
  • Oh got it just needed to do `stlImporter.read(this.getClass().getResource("/mapTest3.stl"));` – Politic Revolutionnaire Mar 21 '17 at 19:55
  • Well done Adam, sorry I wasnt aware of that for eclipse. Yes if that is a resource folder than you can omit the name when using getResource – Edd Mar 21 '17 at 19:59