Deploying JavaFX 2 Application Using Standard Jars Using Ant

I wrote a JavaFx 2 application (using Eclipse on a Windows platform), and now I want to deploy it to a clickable jar file. My application uses the resource code from a separate jar file (in Eclipse, this resource code is a separate project from the JavaFx application project).

With my Ant build.xml, I compiled the code for both the application and the resource code and created two jar files:

  • fxdemo.jar - jar for my JavaFx application code
  • guifw.jar - the jar for the resource code referenced by the JavaFx application.

As a last step (I thought), using JavaFX Ant tasks, I wanted to link these two jar files to the clickable bank that runs my JavaFX application. I tried to do this using the below from my 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> 

However, then when I try to start the application (by clicking on the jar or starting from the command line using java -jar appname.jar), it looks like the application cannot find the main class:

JavaFX startup error

Unable to find class: 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) 

When I examine the created MANIFEST.MF file (in the created jar file), it looks something like I expected.

 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 

... but then again, this does not work, so I did something wrong.

I also tried to include the classes directory (output folders from each of the two Eclipse / projects) by adding:

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

Then it starts my main class (com.demo.main.MainGUI), but it does not work correctly, because it lacks the -D argument, which I tried to specify with:

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

So, if you read this far, my questions are:

  • Why can't the remote find my main class in the indicated jar (fxdemo.jar)?
  • What did I do wrong when I need to specify my -D arguments for the application?

Best wishes

+7
source share
1 answer

I examined / tested the fix provided in the post ( 2012-apr-12 03:32 ) via @Anders Petersson link:

Link from @Anders Petersson

From what I see, this is a workaround: unbudles any used jar file (in my case; guifw.jar and demofx.jar) in the resulting jar file (fxDemoGui.jar), very similar to adding folder classes from my two projects Eclipse (as described in the question).

I adjusted the example in the build.xml file and made it work after a little addition:

 <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 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> 

In the dist- target example from the example, I had to add:

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

Without it, as a result of the jar file, there was no necessary class com.javafx.main.Main and, therefore, could not be launched.

So this solution presents a useful workaround for my question 1)

However, I would appreciate if anyone would come up with a solution on how to save jar files in an intact file in a jar / file.

+2
source

All Articles