从编译的jar运行时的javafx.fxml.LoadException

时间:2015-01-03 00:13:13

标签: java exception javafx resources

当从IntelliJ中本地运行时,我的JavaFX应用程序启动正常,但是当它被编译到jar中然后可供客户端中的用户访问时,我收到以下异常。知道为什么会这样吗?

(07:56:06) javafx.fxml.LoadException:
unknown path:10

(07:56:06)      at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)

(07:56:06)      at javafx.fxml.FXMLLoader.access$700(Unknown Source)
(07:56:06)      at javafx.fxml.FXMLLoader$ValueElement.processAttribute(Unknown
Source)
(07:56:06)      at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttr
ibute(Unknown Source)
(07:56:06)      at javafx.fxml.FXMLLoader$Element.processStartElement(Unknown So
urce)
(07:56:06)      at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unkno
wn Source)
(07:56:06)      at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
(07:56:06)      at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
(07:56:06)      at javafx.fxml.FXMLLoader.load(Unknown Source)
(07:56:06)      at scripts.MassFighter.GUI.Main.start(Main.java:22)
(07:56:06)      at scripts.MassFighter.MassFighter.lambda$showAndWaitGUI$0(MassF
ighter.java:114)
(07:56:06)      at scripts.MassFighter.MassFighter$$Lambda$351/222230364.run(Unk
nown Source)
(07:56:06)      at com.sun.javafx.application.PlatformImpl.lambda$null$164(Unkno
wn Source)
(07:56:06)      at com.sun.javafx.application.PlatformImpl$$Lambda$53/1296636313
.run(Unknown Source)
(07:56:06)      at java.security.AccessController.doPrivileged(Native Method)
(07:56:06)      at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(U
nknown Source)
(07:56:06)      at com.sun.javafx.application.PlatformImpl$$Lambda$52/47061671.r
un(Unknown Source)
(07:56:06)      at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Sou
rce)
(07:56:06)      at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
(07:56:06)      at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown S
ource)
(07:56:06)      at com.sun.glass.ui.win.WinApplication$$Lambda$43/157734934.run(
Unknown Source)
(07:56:06)      at java.lang.Thread.run(Unknown Source)
(07:56:06) Caused by: java.lang.ClassNotFoundException: scripts.MassFighter.GUI.
Controller
(07:56:06)      at java.net.URLClassLoader$1.run(Unknown Source)
(07:56:06)      at java.net.URLClassLoader$1.run(Unknown Source)
(07:56:06)      at java.security.AccessController.doPrivileged(Native Method)
(07:56:06)      at java.net.URLClassLoader.findClass(Unknown Source)
(07:56:06)      at java.lang.ClassLoader.loadClass(Unknown Source)
(07:56:06)      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
(07:56:06)      at java.lang.ClassLoader.loadClass(Unknown Source)
(07:56:06)      ... 20 more

我已将FXML文件作为资源包含在清单中:

<resource>scripts/resources/FighterDesign.fxml</resource>

以下是我用于查找资源并启动应用程序(Main.java)的代码:

private Controller controller;

    @Override
    public void start(Stage primaryStage) throws Exception {
        InputStream in = MassFighter.class.getResourceAsStream("/scripts/MassFighter/GUI/FighterV1.fxml");
        if (in != null) {
            FXMLLoader loader = new FXMLLoader();
            Parent root = loader.load(in);
            primaryStage.setTitle("MassFighter V4");
            primaryStage.setScene(new Scene(root));
            controller = loader.getController();
            controller.initialize();
            primaryStage.setOnCloseRequest(event -> {
                System.out.println("UI Closed - Stopping Script");
                if (Environment.getScript().isRunning()) {
                    Environment.getScript().stop();
                }
                close();
            });
            primaryStage.show();
        } else {
            MassFighter.status = "Input Stream Unavailable"; // the input stream is not null when this error occurs
        }
    }

这是我将FXML文件链接到我的控制器的地方:

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" opacity="0.89" prefHeight="431.0" prefWidth="672.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="scripts.MassFighter.GUI.Controller">

以下是我用来调用start方法的代码:

ui = new Main();
    Platform.runLater(() -> {
        try {
            ui.start(new Stage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    while (setupRunning) {
        Execution.delay(100);
    }
    Platform.runLater(ui::close);

这是我的项目结构:

Structure

任何帮助都表示赞赏,因为我真的很困惑!提前谢谢。

1 个答案:

答案 0 :(得分:1)

我在从Windows上构建的JAR加载资源文件时遇到类似的问题,当它在Linux上运行时,请参阅以下问题的解决方案......

在您的情况下,您需要输入适用的文件/路径名称。请记住,FMXL目录(如果你使用的话)将是:

String fileName = "/fxml/this-under-resources.fxml";

您项目中的内容:

src/main/resources/
  fxml/
    this-under-resources.fxml

该语法工作正常但是,如果您不知道执行路径在哪里;您需要获得与我的HTML示例等效的FXML源路径,这与以下内容非常相似:

罐:/fxml/this-under-resources.fxml

或多或少。然后使用类似于Jetty / HTML案例的代码,即

 URL     baseUrl  = SplitFileServerRunner.class.getResource( baseStr ); 
String  basePath = baseUrl.toExternalForm();

等等。

从那个问题开始,我已经开始了。我只希望在JAR中保留任何演示资源以使用“ fall-back ”和默认视图(或其他资源)。

回想起“外部路径”的一点是,它是一个运行时的东西所以每次/任何时候运行你的JAR文件;答案应该正确。只要您拥有合适的权限和访问权限。祝你好运。