【问题标题】:exporting JavaFX project to jar. file. error when loading FXML file将 JavaFX 项目导出到 jar。文件。加载 FXML 文件时出错
【发布时间】:2015-03-04 04:59:39
【问题描述】:

所以我有这个奇怪的问题。应用程序在 eclipse 项目中工作,但是当我将项目导出到 jar 时。文件并运行它,然后在加载其中一个 fxml 文件时出现 IOexception。

这是一个例外:

javafx.fxml.LoadException: 
com/root/tomaszm/Countdown.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at com.controller.tomaszm.WrittenNumbersController.initializeStoperAndCountdown(WrittenNumbersController.java:162)
    at com.controller.tomaszm.WrittenNumbersController.initialize(WrittenNumbersController.java:114)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at com.model.tomaszm.ChangeTheRoot.initialize(ChangeTheRoot.java:68)
    at com.controller.tomaszm.MainRootController.fireUpTheFeature(MainRootController.java:103)
    at com.controller.tomaszm.MainRootController.access$0(MainRootController.java:96)
    at com.controller.tomaszm.MainRootController$MouseClickListCell$1.handle(MainRootController.java:147)
    at com.controller.tomaszm.MainRootController$MouseClickListCell$1.handle(MainRootController.java:1)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Scene$ClickGenerator.postProcess(Unknown Source)
    at javafx.scene.Scene$ClickGenerator.access$7900(Unknown Source)
    at javafx.scene.Scene$MouseHandler.process(Unknown Source)
    at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
    at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.notifyMouse(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda$38/1657033223.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at com.controller.tomaszm.CountdownController.initialize(CountdownController.java:63)
    ... 46 more

如果您需要一些额外的信息,请告诉我。

编辑: 我发现这是加载source文件夹Files中的字体文件时出现的问题:

try {
            Font registerFont = Font.loadFont(getClass().getClassLoader().getResource("DS-DIGIT.ttf").openStream(), 30);
            labTime.setFont(registerFont);
        } catch (IOException e1) {
            Dialogs.create().title("Exception").masthead(null).message("Couldnt load the font fxml!").showException(e1);
            e1.printStackTrace();
        }

我只是不明白为什么这段代码在项目中工作并在我制作 jar 文件时导致一些奇怪的错误。有什么办法可以正确加载这个字体吗?顺便提一句。我记得在项目的早期阶段,我已经构建了带有字体的 jar 文件,并且它可以正常工作....我在这里感到困惑。

【问题讨论】:

  • DS-DIGIT.ttf 在哪里?
  • @ItachiUchiha 我已经包含了图片。

标签: jar javafx-8 fxml ioexception


【解决方案1】:

如果您在构建项目时没有在代码中创建文件或复制它们,那么很明显它们不在构建文件夹中。

例如,如果您使用 Ant 构建项目,则编写一个目标,将 ttf 文件复制到将创建 jar 文件的同一文件夹中。作为解决方法,您可以自己将其复制到同一文件夹中。

【讨论】:

  • 那么,有没有办法在项目中创建这个字体文件,以后我将把所有东西都放在一个 jar 中,并且不需要额外的文件?
  • 抱歉没时间再看一遍,但看来你已经用自己的方式解决了:)
【解决方案2】:

所以根据 Juce 的信息,我一直在互联网上寻找如何将字体包含到 jar 中然后使用它的解决方案。我找到了这段代码:

InputStream istream = getClass().getResourceAsStream("/resources/SerpentineBolditalic.ttf");
Font myFont = Font.createFont(Font.TRUETYPE_FONT, istream);
myFont = myFont.deriveFont(36.0f);
lblNewLabel.setFont(myFont);

然而,像 Font.TRUETYPE_FONT 这样的一些代码似乎在当前的 Java 版本中贬值了。所以我不得不做出一些改变,现在一切似乎都正常了:

InputStream is = this.getClass().getResourceAsStream("/DS-DIGIT.TTF");
Font uniFont = Font.loadFont(is, 30);
labTime.setFont(uniFont);

【讨论】:

    猜你喜欢
    • 2014-04-27
    • 2018-10-23
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多