【问题标题】:Deploying a JavaFX 2 application with referenced jars, using Ant使用 Ant 使用引用的 jar 部署 JavaFX 2 应用程序
【发布时间】:2012-11-16 06:13:24
【问题描述】:

我已经编写了一个 JavaFx 2 应用程序(在 Windows 平台上使用 Eclipse),现在我想将它部署到一个“可点击”的 jar 文件中。 我的应用程序使用来自单独 jar 文件的资源代码(在 Eclipse 中,此资源代码是与 JavaFx 应用程序项目分开的项目)

使用我的 Ant build.xml,我为应用程序和资源代码编译了代码并创建了两个 jar 文件:

  1. fxdemo.jar - 我的 JavaFx 应用程序代码的 jar
  2. guifw.jar - JavaFx 应用程序引用的资源代码的 jar。

作为最后一步(我想),使用 JavaFX Ant 任务,我想将这两个 jar 文件捆绑到一个“可点击”的 jar 中,以启动我的 JavaFX 应用程序。我尝试使用 build.xml 中的以下摘录来做到这一点。

<target name="deployFx" depends="fxdemo.jar" description="Releases FxDemo">
    <taskdef resource="com/sun/javafx/tools/ant/antlib.xml"      
              uri="javafx:com.sun.javafx.tools.ant"
              classpath=".:${fxgui.javaHome}\lib\ant-javafx.jar"/>

    <copy file="${fxgui.lib_guifw_path}" tofile="delivery/lib/guifw.jar"/>          

    <fx:application id="FxDemoGUI" name="Fx Demo GUI" MainClass="com.demo.main.MainGUI"/>

    <fx:resources id="jars">
        <fx:fileset dir="delivery/lib" includes="fxdemo.jar"/>
        <fx:fileset dir="delivery/lib" includes="guifw.jar"/>
    </fx:resources>

    <fx:jar destfile="deploy/fxDemoGui.jar">
        <!-- Define what to launch  -->
        <fx:application refid="FxDemoGUI"/>

        <fx:platform javafx="2.1+">
            <fx:jvmarg value="-Xms32m"/>
            <fx:jvmarg value="-Xmx32m"/>
            <property name="com.util.fxguifw.setup" value="com/util/fxguifw/demo/demo.properties"/>
            <property name="user.language" value="en"/>
            <property name="user.country" value="GB"/>
            <property name="CSS_ID" value="NIGHT"/>
        </fx:platform>

        <fx:resources>
            <fx:fileset dir="delivery/lib" includes="fxdemo.jar"/>
            <fx:fileset dir="delivery/lib" includes="guifw.jar"/>
        </fx:resources>

        <manifest>
            <attribute name="Implementation-Vendor" value="${fxgui.vendor}"/>
            <attribute name="Implementation-Title" value="${fxgui.title}"/>
            <attribute name="Implementation-Version" value="1.0"/>
        </manifest>

        <fileset dir="delivery"/>

    </fx:jar>

但是,之后当我尝试启动应用程序(通过单击 jar 或使用 java -jar appname.jar 从命令行启动)时,似乎应用程序找不到 Main 类:

JavaFX 启动器错误

找不到类:com.demo.main.MainGUI

C:\Program Files\Java\jdk1.7.0_09\bin>java -jar C:\MEKMAN\Clearcase_Views\wmarekm_ss_gambau\amb_c2_prototype\javafx\prototypeGUI\deploy\fxDemoGui.jar
java.lang.ClassNotFoundException: com.demo.main.MainGUI
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.javafx.main.Main.getAppClass(Main.java:506)
    at com.javafx.main.Main.launchApp(Main.java:622)
    at com.javafx.main.Main.main(Main.java:805)

当我研究创建的 MANIFEST.MF(在创建的 jar 文件中)时,它看起来和我预期的差不多。

Manifest-Version: 1.0
JavaFX-Version: 2.1+
implementation-vendor: MyVendor
implementation-title: MyfirstJavaFxDeploy
implementation-version: 1.0
JavaFX-Application-Class:com.demo.main.MainGUI
JavaFX-Class-Path: fxdemo.jar guifw.jar
Created-By: JavaFXPackager
Main-Class: com/javafx/main/Main

...但话又说回来,它不起作用,所以很明显我做错了什么。

我还尝试通过添加来包含类目录(两个 Eclipse/项目中的每一个的输出文件夹):

<fileset dir="../guifw/classes"/>
<fileset dir="classes"/>

然后,启动器 确实 找到了我的主类 (com.demo.main.MainGUI) 但无法正确运行,因为它缺少我尝试过的 -D 参数指定:

<property name="com.util.fxguifw.setup" value="com/util/fxguifw/demo/demo.properties"/>

所以,如果你已经读到这里,我的问题是:

  1. 为什么启动器在引用的 jar (fxdemo.jar) 中找不到我的主类?
  2. 在为应用程序指定 -D 参数时我做错了什么?

最好的问候

【问题讨论】:

标签: deployment ant javafx-2 executable-jar


【解决方案1】:

我在@Anders Petersson 的链接中研究/测试了帖子 (2012-apr-12 03:32) 中提出的修复:

Link from @Anders Petersson

据我所知,这是一种解决方法,可以在生成的 jar 文件 (fxDemoGui.jar) 中unbudles 任何使用的 jar 文件(在我的情况下为 guifw.jar 和 demofx.jar) ,就像从我的两个 Eclipse 项目中添加类文件夹(如问题中所述)。

我将示例调整为我的 build.xml 并在稍微添加后让它工作:

<target name="dist" depends="fxdemo.jar">

    <taskdef resource="com/sun/javafx/tools/ant/antlib.xml"      
                              uri="javafx:com.sun.javafx.tools.ant"
                              classpath=".:${fxgui.javaHome}\lib\ant-javafx.jar"/>

    <copy file="${fxgui.lib_guifw_path}" tofile="delivery/lib/guifw.jar"/>

    <fx:jar destfile="predeploy/fxDemoGui.jar">
        <!-- ADDITION -> Adds the Launcher's Main class to the resulting jar (com.javafx.main.Main)! -->
        <fx:application id="FxDemoGUI"
                        name="Fx Demo GUI"
                        mainClass="com.demo.main.MainGUI"/>

        <fileset dir="delivery"/>
    </fx:jar>
</target>

在示例中的 dist-target 中,我必须添加:

<fx:application id="FxDemoGUI"
                name="Fx Demo GUI"
                mainClass="com.demo.main.MainGUI"/>

没有它,生成的 jar 文件没有必要的 com.javafx.main.Main 类,因此无法启动。

因此,此解决方案为我的问题 1) 提供了一个有用的解决方法

不过,如果有人想出一个解决方案来说明如何在生成的 jar/文件中保持 jar 文件的完整性,我将不胜感激。

【讨论】:

    猜你喜欢
    • 2013-09-19
    • 1970-01-01
    • 2011-10-08
    • 2021-02-25
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 2019-04-26
    相关资源
    最近更新 更多