如何在同一个项目中找到文件的路径?

bxgwgixi  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(352)

我有一个JavaFX应用程序,我将它从纯java项目迁移到maven,现在我在.fxml文件的路径上遇到了问题。正如您在下面看到的,我需要main.java中的sample.fxml。我试过不同的方法,但都没用。你能看一下吗?

以下是我的尝试:

System.out.println(getClass().getResource("src/main/java/sample/fxml/sample.fxml"));
    System.out.println(getClass().getResource("/java/sample/fxml/sample.fxml"));
    System.out.println(getClass().getResource("sample/fxml/sample.fxml"));
    System.out.println(getClass().getResource("/sample/fxml/sample.fxml"));
    System.out.println(getClass().getResource("fxml/sample.fxml"));
    System.out.println(getClass().getResource("sample.fxml"));

    System.out.println(getClass().getResourceAsStream("src/main/java/sample/fxml/sample.fxml"));
    System.out.println(getClass().getResourceAsStream("/java/sample/fxml/sample.fxml"));
    System.out.println(getClass().getResourceAsStream("sample/fxml/sample.fxml"));
    System.out.println(getClass().getResourceAsStream("/sample/fxml/sample.fxml"));
    System.out.println(getClass().getResourceAsStream("fxml/sample.fxml"));
    System.out.println(getClass().getResourceAsStream("sample.fxml"));

所有这些都返回null。

wpx232ag

wpx232ag1#

我找到了答案。

Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

将sample.fxml移到资源文件夹时正在工作。

相关问题