2

我正在使用 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 但到目前为止还没有任何解决方案。

谢谢你的帮助!

4

3 回答 3

3

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

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 资源管理器中双击或输入

java -jar TestApp.jar

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

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

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

希望这对其他人有帮助。

于 2011-07-24T12:30:26.767 回答
0

在 Netbeans 中,在项目 => 属性 => 构建 => 打包下,您是否选中了“复制依赖库”?

于 2011-07-21T00:00:09.647 回答
0

您需要告诉 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-06-10T22:03:42.577 回答