在java中更改javafx文件的路径

时间:2018-06-18 16:29:04

标签: java maven javafx maven-2

Project Dirs Structure

我有一个Maven项目,我正在尝试使用javafx(我正在使用Eclipse) 我想改变文件的路径。我有

Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));

这适用于下一个文件夹结构

src
  main
    java
      com
        projectFolder
           main.java
      application
        Main.fxml

但我想要像

这样的东西
src
  main
    java
      com
        projectFolder
           main.java
        application
           Main.fxml

我已尝试过Parent root = FXMLLoader.load(getClass().getResource("/com/application/Main.fxml"));但无效。

我收到了下一个错误

   [JavaFX Application Thread] ERROR com.application.Main - javafx.fxml.LoadException: 
/C:/Users/user/workspace/git/project/target/classes/com/application/Main.fxml:15

有人知道它有什么问题吗?

3 个答案:

答案 0 :(得分:0)

我不建议混淆文件结构...

但在这种情况下,我认为您需要使用资源的相对路径:../application/Main.fxml

getClass().getResource()期望资源与调用类位于同一目录中。它不是,所以你需要Java用..

备份一个目录

答案 1 :(得分:0)

假设您和我确实具有相同的项目结构,如下图所示:

enter image description here

要加载FXML文件,您应该使用完整路径,如下例所示:

package main.java.com.projectFolder;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/main/java/com/application/Main.fxml"));

        Parent root = loader.load();
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}

现在,如果您实际使用的相对路径如下:

FXMLLoader loader = new FXMLLoader(this.getClass().getResource("../application/Main.fxml"));

只要您通过Eclipse运行时环境编译和运行代码,上述操作也会起作用。将项目导出到JAR文件并尝试执行它的那一刻,您将得到从一开始就获得的相同错误。你可以在类似的帖子中读到这个:It runs OK in Eclipse but not able to run as .jar file

编辑:对于maven项目,您应该考虑将FXML文件重定位到src / main / resources /文件夹。你可以在这篇文章中找到一个:JavaFX and maven: NullPointerException: Location is required

答案 2 :(得分:0)

除了其他答案之外,这里还有一个帮助方法,当您对文件的真实路径感到困惑时,可以咨询。

import java.io.File;

public class FilePathFinder {

    public static void main(String[] args) {
        FilePathFinder.getPath(new File(System.getProperty("user.dir")), "/application/Main.fxml");
    }

    static void getPath(File root, String fileName) {
        File[] files = root.listFiles();
        for (File file : files) {
            if (file.isDirectory()) getPath(file, fileName);

            if(file.getAbsolutePath().contains(fileName))
                System.out.println(file.getAbsolutePath());
       }
    }
}

如何运作?

  • System.getProperty("user.dir")返回用户正常工作 目录
  • getPath(File root, String fileName)方法搜索 以递归方式在项目目录及其子目录中找到目标文件,然后打印出绝对路径

修改

根据下面的一些评论,有些人无法理解这是如何工作的,因为他们无法理解传递给方法的File的目的是什么。 我重新考虑了代码只是为了让它更清晰,尽管它做同样的事情!

import java.io.File;

public class FilePathFinder {

    public static void main(String[] args) {
        // you only need to pass the name of the file
        // in case you could not understand the Structure Tree!
        // then the result (the full path) would help you to understand it!
        FilePathFinder.getPath("Main.fxml");
    }

    static void getPath(String fileName){ // just a wrapper method!
        search(new File(System.getProperty("user.dir")), fileName);
    }

    static void search(File root, String fileName) {
        File[] files = root.listFiles();
        for (File file : files) {
            if (file.isDirectory()) search(file, fileName);

            if(file.getAbsolutePath().contains(fileName))
                System.out.println(file.getAbsolutePath());
       }
    }
}