将JavaFX项目导出到jar。文件。加载FXML文件时出错

时间:2015-03-04 04:59:40

标签: jar javafx-8 fxml ioexception

所以我有这个奇怪的问题。应用程序在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文件,并且它没有问题....我在这里赞成。

enter image description here

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

基于来自 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);