【问题标题】:JavaFx 2.0 application referencing external jars引用外部 jar 的 JavaFx 2.0 应用程序
【发布时间】:2011-10-08 16:17:58
【问题描述】:

我正在使用 Netbeans 7 开发 JavaFx 2.0 应用程序。 主应用程序引用另一个通过右键单击“库”文件夹并选择“添加项目...”添加的类库项目。从 netbeans 执行应用程序工作正常。

通过“清理并构建”将其部署到 jar 文件并尝试通过控制台执行它时

java -jar TestApp.jar

我明白了

Exception in thread "JavaFX-Launcher" java.lang.NoClassDefFoundError: net/pmoule/SomeClass
...

我的应用程序的 dist/lib 文件夹包含引用的库。所以恕我直言,一切都应该没问题。查看包含在我的应用程序 jar 中的 Manifest.MF 我得到了这个

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_23-b05 (Sun Microsystems Inc.)
Implementation-Vendor: pmoule
Implementation-Title: TestApp
Implementation-Version: 1.0
Main-Class: com/javafx/main/Main
JavaFX-Application-Class: testapp.TestApp
JavaFX-Version: 2.0

我的课程路径在哪里?如何让 Netbeans 添加正确的类路径?

我尝试通过编辑 jar 中包含的手动将其添加到 Manifest.MF

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_23-b05 (Sun Microsystems Inc.)
Implementation-Vendor: pmoule
Implementation-Title: TestApp
Implementation-Version: 1.0
Class-Path: lib/MyLib.jar        //THIS IS NEW
Main-Class: com/javafx/main/Main
JavaFX-Application-Class: testapp.TestApp
JavaFX-Version: 2.0

没有成功,同样的错误。

JavaFX 2.0 SDK 提供的所有示例都可以通过在 WindowsExplorer 中双击或从控制台输入例如来工作

java -jar PathAnimation.jar

但这些示例中没有任何一个引用了外部 jar。

一些研究让我想到了这个问题:Netbeans JavaFX 2.0 Application 但目前还没有任何解决方案。

感谢您的帮助!

【问题讨论】:

  • 更新:为了满足Oracle列出的要求,我将平台更改为JDK 6_26;我将此行添加到项目的 jfx-impl.xml <attribute name="Class-Path" value="${jar.classpath}"/> 中的 fxjar-task 中。现在类路径包含在 manifest.mf 中。错误还是一样的:(

标签: javafx-2


【解决方案1】:

在 Netbeans 中,在 project => properties => Build => Packaging 下,您是否选中了“Copy Dependent Libraries”?

【讨论】:

  • 感谢提示,该选项已选中。正如我所写,项目的 dist/lib 文件夹在执行“清理和构建”后包含有问题的库。
  • 您是否按照这些指示进行 FXDeployment? download.oracle.com/javafx/2.0/deployment/jfxpub-deployment.pdf
  • 是的,我按照部署指南中的说明进行操作。我什至更新了 JavaFX 的 Netbeans 插件并更新了项目的构建文件。没有任何外部 jar 的部署工作正常。仅在包含一些外部 jar 时才会出现此问题。
【解决方案2】:

自己找到了一个可行的解决方案。

dist/lin 文件夹中的所有外部库的大小均为 0kb。所以这个例外当然是正确的。

为了让我的应用程序运行,我在项目的 jfx-impl.xml 中执行了以下操作:

将类路径添加到 manifest.mf

<fxjar destfile="${jfx.deployment.dir}/${jfx.deployment.jar}" applicationClass="${main.class}" >
             <fileset dir="${build.classes.dir}"/>
              <manifest>
               <attribute name="Implementation-Vendor" value="${application.vendor}"/>
               <attribute name="Implementation-Title" value="${application.title}"/>
<!-- NEW -->   <attribute name="Class-Path" value="${jar.classpath}"/> <!-- NEW -->
               <attribute name="Implementation-Version" value="1.0"/>
              </manifest>
    </fxjar>

为 Web 部署创建输出目录

<property name="jfx.deployment.web.dir" location="${jfx.deployment.dir}/web" />
<mkdir dir="${jfx.deployment.web.dir}" />

为 fxdeploy 任务设置输出目录

<fxdeploy width="${jfx.applet.width}" height="${jfx.applet.height}"
              outdir="${jfx.deployment.web.dir}" <!-- NEW DIR -->
              embedJNLP="true"
              outfile="${application.title}">
        <info title="${application.title}"
              vendor="${application.vendor}"/>
        <application name="${application.title}"
                     appclass="${main.class}"/>
        <resources type="eager">
           <fileset dir="${jfx.deployment.web.dir}"> <!-- NEW DIR -->
              <include name="${jfx.deployment.jar}"/>
              <include name="lib/*.jar"/>
              <exclude name="**/jfxrt.jar"/>
           </fileset>
        </resources>
</fxdeploy>

现在,我可以部署我的桌面应用程序并通过在 Windows 资源管理器中双击或输入来执行 ist

java -jar TestApp.jar

我新创建的 web-dir 的内容仍然存在一些问题。

  1. TestApp.jar 未复制到 dist/web
  2. 引用的外部 jar 不会复制到 dist/web

这对我来说很好,稍后会修复。

希望这对其他人有帮助。

【讨论】:

    【解决方案3】:

    您需要告诉 fx:jar 任务您的类路径依赖项是什么:

    <fxjar destfile="${jfx.deployment.dir}/${jfx.deployment.jar}" applicationClass="${main.class}" >
          <fileset dir="${build.classes.dir}"/>
          <manifest>
            <attribute name="Implementation-Vendor" value="${application.vendor}"/>
            <attribute name="Implementation-Title" value="${application.title}"/>
            <attribute name="Implementation-Version" value="1.0"/>
          </manifest>
    
          <!-- Setup the classpath for the generated .jar here -->
          <fx:resources>
            <fx:fileset type="jar" dir="lib" includes="MyLib.jar"/>
          </fx:resources>
    </fxjar>
    

    您还需要在 fx:deploy 任务中使用 fx:resources 标签,而不仅仅是资源。这应该可以解决您的答案中剩下的最后两个问题。

    【讨论】:

      猜你喜欢
      • 2013-09-19
      • 2012-11-16
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 2018-04-25
      • 2012-02-22
      • 1970-01-01
      相关资源
      最近更新 更多