File JavaFX文件没有';尽管文件位于正确位置,但不存在异常

File JavaFX文件没有';尽管文件位于正确位置,但不存在异常,file,exception,javafx,import,stl,File,Exception,Javafx,Import,Stl,正如您在这里看到的,该文件在项目中的源文件夹中显然可用,但是当运行程序时,我收到的文件不存在异常。以下是我发布的完整错误: 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.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)

问题是您使用的是绝对路径。如果要使用绝对路径,则需要提供根目录(/或C:/)的完整路径

如果要使用相对路径
music/untitled.stl
,则需要将当前工作目录指向测试文件夹

从代码加载资源的一种方法是将资源放在
src
文件夹中的
resource
文件夹中,然后使用
getClass().getResourceAsStream(“/resource/resource\u name”)

因此,如果您要将音乐文件夹移动到src文件夹中,您将能够像这样读取文件:

getClass().getResourceAsStream(“/music/untitled.stl”)


请注意在开头如何使用“/”。它在这种情况下工作,因为getResourceAsStream负责使用类加载器解析到src目录的路径

请注意绝对路径和相对路径之间的差异。请参阅:and
File File File=new文件(this.getClass().getResource(“/music/mapTest3.stl”).toExternalForm());stlImporter.read(文件)我尝试使用此选项,但收到空指针异常。请在下面的答案中提供完整的代码片段。您是否将音乐文件夹移动到了src文件夹中?在项目中的“极简主义”文件夹旁边。我用这个.getClass().getResourceAsStream(“/resource/test.txt”)进行了测试,其中资源位于我的src文件夹中,我得到了一个有效的流。如果要使用文件而不是流,请使用getClass().getResource(“/music/mapTest3.stl”).getFile();您还可以在这里查看一个示例:music是一个资源文件夹,而Eclipse的组织方式是在src文件夹中没有资源文件夹,而是在它旁边。通过使用
stlImporter.read(新文件(System.getProperty(“user.dir”)+“/music/mapTest3.stl”)使其工作那一行,但我知道当我将它导出到
jar
文件时,它将不起作用。哦,我只需要执行
stlImporter.read(this.getClass().getResource(“/mapTest3.stl”)