JavaFx 2.0应用程序引用外部jar

时间:2011-07-19 16:00:31

标签: javafx-2

我正在使用Netbeans 7开发JavaFx 2.0应用程序。 主应用程序通过右键单击“Libraries”文件夹并选择“Add Project ...”来引用另一个类库项目。从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 但到目前为止还没有任何解决方案。

感谢您的帮助!

3 个答案:

答案 0 :(得分: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资源管理器中双击或输入

来执行ist
java -jar TestApp.jar

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

  1. TestApp.jar不会被复制为zo dist / web
  2. 引用的外部jar不会复制到dist / web
  3. 这对我来说很好,并会在一段时间后修复。

    希望这有助于其他任何人。

答案 1 :(得分:0)

在Netbeans中,在project =&gt;下properties =&gt; Build =&gt;打包,你检查“复制依赖库”吗?

答案 2 :(得分: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标记,而不仅仅是资源。这应该可以解决你答案中留下的最后两个问题。

相关问题