JavaFX 项目在导出到可运行的 .jar 时中断? FXMLLoader 相关吗?

标签 java eclipse javafx jar

我一直在尝试将 JavaFX 项目导出为 .jar,但每当我运行它时,我都会收到 NullPointerException;据说,它找不到 FXML。我不知道如何处理这个问题,我的项目结构有问题吗?我查看了:FXML layout not loaded when run jar outside Eclipse ,但该解决方案似乎对我不起作用,并且存在不同的问题。

该项目托管于:https://github.com/Sunquyman/GameOfLife/tree/master/src

尝试运行时收到的堆栈跟踪:

java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at main.Main.start(Main.java:21)
    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.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
    at java.lang.Thread.run(Thread.java:745)

真的很困惑为什么会发生这种情况;如果它在 Eclipse 中运行良好,为什么导出时会损坏?

提前致谢!

最佳答案

首先,您必须创建一个资源文件夹,如下所示:

要执行此操作,请右键单击项目 -> 新建 -> 资源文件夹 -> 根据需要命名,但资源是一个不错的选择

enter image description here

然后我修改了您的代码,如下所示:

主类:

public class Main extends Application {
@Override
public void start(Stage primaryStage) {
    try {

        //You have to Load the font before using it into css!!
        Font.loadFont(getClass().getResourceAsStream("/resources/fonts/8bit.ttf"), 14);
        Scene scene = new Scene(new GUIController());
        scene.getStylesheets().add(getClass().getResource("/view/application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.setTitle("Game of Life Settings");
        primaryStage.show();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

GUIController:请记住,在 .fxml 文件中,我使用了 root 和 !default Controller,所以它现在看起来像这样(使用 SceneBuilder )。基本上这样做,您可以使用 GUIController 类一次。

enter image description here

public class GUIController extends AnchorPane implements Initializable {

..........

private final String lotOn = "/music/soundfx/lensoftruth_on.mp3";
private final String lotOff = "/music/soundfx/lensoftruth_off.mp3";

 .......

//Constructor
public GUIController(){
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/GUI.fxml"));
    loader.setRoot(this);
    loader.setController(this);

    try {
        loader.load();
    } catch (IOException e) {
        e.printStackTrace();
    }
  }


.......

 }

关于 CSS: 最后,我修改了 css 文件,因为 css 字体在添加 css 之前已加载到代码中( check this here ):

@font-face {

-fx-font-family: "8bit";

}

最后关于/:

如果为类调用 getResource() 并且不添加/,则该路径将被视为相对于该类的包。 Check this here

Are you happy? :)

关于JavaFX 项目在导出到可运行的 .jar 时中断? FXMLLoader 相关吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37380297/

相关文章:

java - 我的循环在 Java 中设置正确吗?

java - JTextPane 每次我插入字符串时都会添加大空格

eclipse - Spring Web-用 WebApplicationInitializer 替换 web.xml 给我 404

java - @Nullable 与泛型 (Eclipse)

JavaFx setText 将字段设置为 int

java - 在java fx中使用堆栈和循环绘制树

java - 是否有必要将 Enum 成员声明为最终成员以实现不变性?

java - 部署应用程序会跳过资源文件

java - 从基于 MVC 的 GUI 访问数据访问层

JAVA如何创建一维和多维数组