Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在同一项目中找到文件的路径?_Java_Maven_File_Intellij Idea - Fatal编程技术网

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

Java 如何在同一项目中找到文件的路径?,java,maven,file,intellij-idea,Java,Maven,File,Intellij Idea,我有一个JavaFX应用程序,我将它从纯java项目迁移到maven,现在我对.fxml文件的路径有问题。如下所示,我需要Main.java中的sample.fxml。我尝试了不同的方法,但没有任何效果。你能看一下吗 以下是我尝试过的: System.out.println(getClass().getResource("src/main/java/sample/fxml/sample.fxml")); System.out.println(getClass

我有一个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。

我找到了答案

Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
当我们将sample.fxml移到资源文件夹时,它正在工作。

我找到了答案

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

当我们将sample.fxml移到资源文件夹时,它正在工作。

您可以使用2个点(..)返回文件夹。您应该使用“./sample/fxml/sample.fxml”我希望,这也不起作用。System.out.println(getClass().getResourceAsStream(“../sample/fxml/sample.fxml”);System.out.println(getClass().getResource(“../sample/fxml/sample.fxml”);返回空值。您可以使用2个点(..)返回文件夹。您应该使用“./sample/fxml/sample.fxml”我希望,这也不起作用。System.out.println(getClass().getResourceAsStream(“../sample/fxml/sample.fxml”);System.out.println(getClass().getResource(“../sample/fxml/sample.fxml”);返回空值