Compile JavaFX Code using ANT

2019-02-09 19:24发布

I have following installed on my system -

 Java version "1.7.0_09"
 JavaFX 2.0 SDK 
 NetBeans 7.2.1

When I am trying to compile the code using ANT it showing me error message -

Could not load definitions from resource com/sun/javafx/tools/ant/antlib.xml. It could not be found. 

Build.XML contains

<project name="XYZ"  default="XYZ" basedir="XYZ" xmlns:fx="javafx:com.sun.javafx.tools.ant">
    <description>
        simple example build file
    </description>

  <!-- set global properties for this build -->
    <property name="srcXYZGenerator" location="src/XYZGenerator"/>


    <property name="classpath" location="lib/XYZLib.jar;lib/ABC.jar;lib/IJK.jar;"/>


    <target name="init">
        <!-- Create the time stamp -->
        <tstamp/>
        <!-- Create the build directory structure used by compile -->
        <mkdir dir="${buildXYZ}"/>  

    </target> 

    <target name="XYZ" depends="init">
    <!-- Compile the java code from ${src} into ${build} -->
    <taskdef resource="com/sun/javafx/tools/ant/antlib.xml"      
    uri="javafx:com.sun.javafx.tools.ant" classpath=".:C:\Program Files\Java\jdk1.7.0_09\lib\ant-javafx.jar"/>
    <javac classpath="${classpath}" srcdir="${srcXYZ}" destdir="${buildXYZ}"/>
    </target>


</project>  

Other Compilation Errors:

[javac] C:\Users\JavaUser4\Desktop\XYX2012.12FX\XYZ\src\Utility\net\XYZ\javafx\queue\DefaultStatisticsHandlerController.java:19: error: package javafx.scene does not exist
[javac] import javafx.scene.Node;
[javac]                    ^
[javac] C:\Users\JavaUser4\Desktop\XYZ2012.12FX\XYZ\src\Utility\net\XYZ\javafx\queue\DefaultStatisticsHandlerController.java:20: error: package javafx.scene.control does not exist
[javac] import javafx.scene.control.Button;
[javac]                            ^
[javac] C:\Users\JavaUser4\Desktop\XYZ2012.12FX\XYZ\src\Utility\net\XYZ\javafx\queue\DefaultStatisticsHandlerController.java:21: error: package javafx.scene.control does not exist
[javac] import javafx.scene.control.Label;
[javac]                            ^
[javac] C:\Users\JavaUser4\Desktop\XYZ2012.12FX\XYZ\src\Utility\net\XYZ\javafx\queue\DefaultStatisticsHandlerController.java:22: error: package javafx.scene.input does not exist
[javac] import javafx.scene.input.MouseEvent;

1条回答
forever°为你锁心
2楼-- · 2019-02-09 19:54

Update for Java 8

In Oracle Java 8, jfxrt.jar is on the class path by default, so you don't need to explicitly add it to the class path as described in this answer. Adding jfxrt.jar to the classpath is only necessary for Java 7.


The compilation problem was because jfxrt.jar from jre\lib was not set in the classpath.

The taskdef error was caused because the path specified to ant-javafx.jar value does not point to a valid file on your filesystem.

I thought there might also be issues around using a : rather than a ; in seperating paths and in using / rather than \ to specify paths, but ant seems pretty forgiving about those things, so they likely don't matter at all.

I created a sample project based on a very slightly modified version of your build script which worked for me (no taskdef warning and no compilation errors). It is set up for jdku9 windows 64bit. If you are using a different version of the jdk, you will need to adjust the paths in the script appropriately. Check which bit version of the jdk you have installed - if it is 64 bit then you use C:\Program Files\Java\..., if it is 32 bit then you use C:\Program Files (x86)\Java\...

The resultant application for the sample project is executable using:

java -classpath "c:\Program Files (x86)\Java\jdk1.7.0_09\jre\lib\jfxrt.jar;XYZ\lib\ScenicView.jar;XYZ\build\XYZ" FriendFinder

Note that the build script in the sample is very basic and is really just provided as a starter script to get you going. You are best off also adding ant tasks for further deployment tasks (like fx:deploy) to ensure a properly packaged application which supports features like a click-to-run jar. Either that, or use NetBeans and let it generate appropriate ant build files for you.

The modified ant script I used to build (with ant 1.8.4) the sample application I linked is:

<project name="XYZ" default="XYZ" basedir="XYZ" xmlns:fx="javafx:com.sun.javafx.tools.ant">    
    <property name="srcXYZ" location="src/XYZ"/>
    <property name="buildXYZ" location="build/XYZ"/>    
    <property name="classpath" location="C:\Program Files (x86)\Java\jdk1.7.0_09\jre\lib\jfxrt.jar;lib\ScenicView.jar"/>

    <target name="init">
        <tstamp/>
        <mkdir dir="${buildXYZ}"/>  
    </target> 

    <target name="XYZ" depends="init">
        <taskdef resource="com/sun/javafx/tools/ant/antlib.xml"      
    uri="javafx:com.sun.javafx.tools.ant" classpath=".:C:\Program Files (x86)\Java\jdk1.7.0_09\lib\ant-javafx.jar"/>    
        <javac includeantruntime="false" classpath="${classpath}" srcdir="${srcXYZ}" destdir="${buildXYZ}"/>
    </target>
</project>  
查看更多
登录 后发表回答