java 使用 ANT 编译 JavaFX 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13742149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:50:45  来源:igfitidea点击:

Compile JavaFX Code using ANT

javaantbuildcompilationjavafx-2

提问by Ashish Pancholi

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 -

当我尝试使用 ANT 编译代码时,它向我显示错误消息 -

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

Build.XML contains

Build.XML 包含

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

回答by jewelsea

Update for Java 8

Java 8 更新

In Oracle Java 8, jfxrt.jaris 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.jarto the classpath is only necessary for Java 7.

在 Oracle Java 8 中,jfxrt.jar默认情况下位于类路径上,因此您无需按照本答案中的说明将其显式添加到类路径中。jfxrt.jar只有 Java 7 才需要添加到类路径。



The compilation problem was because jfxrt.jarfrom jre\libwas not set in the classpath.

编译问题是因为jfxrt.jarfromjre\lib未在类路径中设置。

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

taskdef 错误是因为指定给ant-javafx.jarvalue的路径没有指向文件系统上的有效文件。

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.

我认为在单独的路径中使用 a:而不是 a;以及使用/而不是\指定路径也可能存在问题,但是 ant 似乎对这些事情非常宽容,因此它们可能根本不重要。

I created a sample projectbased 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\...

我基于对我有用的构建脚本的稍微修改过的版本创建了一个示例项目(没有 taskdef 警告,也没有编译错误)。它是为 jdku9 windows 64bit 设置的。如果您使用的是不同版本的 jdk,则需要适当调整脚本中的路径。检查您安装的 jdk 的哪个位版本-如果是 64 位,则使用C:\Program Files\Java\...,如果是 32 位,则使用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.

请注意,示例中的构建脚本非常基础,实际上只是作为入门脚本提供给您。您最好还为进一步的部署任务(如fx:deploy添加 ant 任务,以确保正确打包的应用程序支持点击运行 jar 等功能。要么,要么使用 NetBeans,让它为您生成适当的 ant 构建文件。

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

我用来构建(使用ant 1.8.4)我链接的示例应用程序的修改后的 ant 脚本是:

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