JavaFx 应用程序在 IDE 上运行良好,但不能作为 jar 文件运行

标签 java javafx javafx-8

首先,我的第一个问题。发现类似的问题,但要么没有解决,要么不一样。所以就这样了。我有一个 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$1(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$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(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$4(WinApplication.java:186)
    ... 1 more
Exception running application application.Main

最佳答案

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

这是你应该如何加载它:

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

这是您在资源文件夹中组织 fxml View 的方式: Check this pic

关于JavaFx 应用程序在 IDE 上运行良好,但不能作为 jar 文件运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62183691/

相关文章:

java - 锁定条件和特定值

JavaFX 无法导入

java - 在JavaFX中,有没有办法先显示选项卡名称,然后显示图形?

JavaFX 窗口拖动突然且不平滑

javafx - 按 F4 禁用组合框下拉菜单

javafx-2 - 我如何在 Javafx2 中使用 jfreeChart

css - ProgressIndicator透明背景

java - 使用 MockRestServiceServer 测试 HttpRequestExecutingMessageHandler

Java实现构建器模式的最佳方式

Java - 向 JFrame 添加组件