JavaFx 应用程序在 IDE 上工作正常但不能作为 jar 文件

JavaFx application works fine on IDE but not as a jar file

首先,我的第一个问题在这里。发现了类似的问题,但要么没有解决,要么不一样。所以就这样了。我有一个 javafx 桌面应用程序。它在 intellij 上工作得很好,但是当我构建一个 .jar 文件时,它却没有。我不明白的是为什么它试图在 C: 目录中找到它。它不应该在 jar 文件中寻找吗?我该如何解决这个问题?

当我在命令提示符下尝试 运行 时,它说:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: C:\Users\MYUSERNAME\src\resources\loginPage.fxml (The 
system cannot find the path specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
    at java.net.URL.openStream(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2440)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    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 application.Main.start(Main.java:24)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater(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(WinApplication.java:186)
    ... 1 more
Exception running application application.Main

我认为您没有正确复制 .jar 文件中的资源,或者您错误地调用了它。

你应该这样加载它:

    stage.setScene(new Scene(FXMLLoader.load(getClass().getResource("/views/ExampleView.fxml"))));

这是您应该如何在资源文件夹中组织您的 fxml 视图: Check this pic