使用 ANT 编译 JavaFX 代码 [英] Compile JavaFX Code using ANT
问题描述
我的系统上安装了以下内容 -
I have following installed on my system -
Java version "1.7.0_09"
JavaFX 2.0 SDK
NetBeans 7.2.1
当我尝试使用 ANT 编译代码时,它向我显示错误消息 -
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 包含
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>
其他编译错误:
[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;
推荐答案
Java 8 更新
在 Oracle Java 8 中,jfxrt.jar
默认位于类路径上,因此您无需按照本答案中的说明将其显式添加到类路径中.将 jfxrt.jar
添加到类路径中,只有 Java 7 才需要.
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.
编译问题是因为 jre\lib
中的 jfxrt.jar
未在类路径中设置.
The compilation problem was because jfxrt.jar
from jre\lib
was not set in the classpath.
taskdef 错误是因为指定给 ant-javafx.jar
值的路径没有指向文件系统上的有效文件.
The taskdef error was caused because the path specified to ant-javafx.jar
value does not point to a valid file on your filesystem.
我认为在分隔路径中使用 :
而不是 ;
以及使用 /
而不是 也可能存在问题>\
来指定路径,但 ant 似乎对这些事情很宽容,所以它们可能根本不重要.
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.
我创建了一个 示例项目构建对我有用的脚本(没有 taskdef 警告,也没有编译错误).它是为 jdku9 windows 64bit 设置的.如果您使用的是不同版本的 jdk,则需要适当调整脚本中的路径.检查您安装的 jdk 的哪个位版本 - 如果是 64 位,则使用 C:\Program Files\Java\...
,如果是 32 位,则使用 C:\Program Files (x86)\Java\...
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
请注意,示例中的构建脚本非常基础,实际上只是作为入门脚本提供给您.您最好也为进一步的部署任务添加 ant 任务(像 fx:deploy
) 以确保正确打包的应用程序支持点击运行 jar 等功能.要么,要么使用 NetBeans,让它为您生成适当的 ant 构建文件.
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.
我用来构建示例应用程序的修改后的 ant 脚本(使用 ant 1.8.4)我链接的是:
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>
这篇关于使用 ANT 编译 JavaFX 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!