无法使用 Ant 构建 JavaFX 应用程序

Cannot build JavaFX application using Ant

我需要为 ant 创建构建文件来构建我的 JavaFX 项目,我搜索了很多,但没有任何帮助。它仍然显示错误但编译。当我尝试 运行 jar 文件时出现异常。我尝试了不同的路径,但仍然没有。
这是我的 build.xml。

  <?xml version="1.0" encoding="UTF-8" ?>
<project name="JDBC Ant Project" default="default" basedir="." xmlns:**fx="javafx:com.sun.javafx.tools.ant"**(Uri is not registered)>
    <property name="src.dir" location="src"/>
    <property name="build.dir" location="classes"/>
    <property name="out.dir" location="out"/>
    <property name="docs.dir" location="docs"/>
    <property name="bin.dir" location="bin"/>
    <property name="lib.dir" location="lib"/>
    <property name="jar.name" value="javafxtest.jar"/>
    <property name="sdk.dir" location="/usr/lib/jvm/java-8-oracle/lib"/>
    <target name="default" depends="clean,compile">
        <path id="fxant">
            <filelist>
                <file name="/usr/lib/jvm/java-8-oracle/lib/ant-javafx.jar"/>
                <file name="/usr/lib/jvm/java-8-oracle/jre/lib/ext/jfxrt.jar"/>
            </filelist>
        </path>
        <*taskdef*(Failed to load types) resource="com/sun/javafx/tools/ant/antlib.xml"
                 uri="javafx:com.sun.javafx.tools.ant"
                 classpathref="fxant"/>

        <fx:**application** id="HelloWorldID"
                        name="JDBC Java FX"
                        mainClass="Main"/>

        <fx:resources id="appRes">
            <fx:fileset dir="${out.dir}" includes="HelloWorld.jar"/>
        </fx:resources>

        <fx:jar destfile="${out.dir}/${jar.name}">
            <fx:application refid="HelloWorldID"/>
            <fx:resources refid="appRes"/>
            <fileset dir="${build.dir}"/>
        </fx:jar>

        <fx:**deploy width**="300" **height**="250"
                   **outdir**="." **embedJNLP**="true"
                   **outfile**="helloworld">

            <fx:**application** refId="HelloWorldID"/>

            <**fx:resources** refid="appRes"/>

            <**fx:info** title="JavaFX Hello World Application"
                     vendor="Oracle Corporation"/>

        </fx:**deploy**>

    </target>

    <target name="clean">
        <echo>Performing clean target</echo>
        <delete dir="${build.dir}"/>
        <delete dir="${docs.dir}"/>
        <delete dir="${out.dir}"/>
    </target>

    <target name="init">
        <echo>Performing init target</echo>
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${docs.dir}"/>
        <mkdir dir="${out.dir}"/>
    </target>

    <target name="compile" depends="clean, init">
        <echo>Performing compiling</echo>
        <javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}">
            <classpath refid="build.classpath"/>
        </javac>

    </target>
    <path id="build.classpath">
        <fileset dir="${lib.dir}" casesensitive="no">
            <include name="**/*.jar"/>
        </fileset>
    </path>
    <target name="javadoc" depends="compile">
        <javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
            <!-- Define which files / directory should get included, we include all -->
            <fileset dir="${src.dir}">
                <include name="**"/>
                <exclude name="**/resources/**"/>
            </fileset>
        </javadoc>
    </target>

    <target name="build" depends="compile">
        <jar destfile="${out.dir}/${jar.name}" basedir="${build.dir}">
            <manifest>
                <attribute name="Main-Class" value="Main"/>
            </manifest>
        </jar>
    </target>

    <target name="main" depends="compile, build, javadoc">
        <description>Main target</description>
    </target>

</project>

** 在我的 IDEA 中完全 "red" (Intellij IDEA.
* 带下划线。

但是它仍然使用 ant -f build.xml 构建。但是当我尝试 运行 jar 文件时,我遇到了下一个异常。 应用程序启动方法异常

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
.....
.....
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2438)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2413)
    at Main.start(Unknown Source)

关于使用 JavaFX ant 任务复制资源的更新

There is no folder with fxml, css and other files in my output jar file. If put it manually everything works, how to say ant to exlictly include folder ?

基于 Java 部署教程 JavaFX Ant 任务 HelloWorld build.xml sample,并对其进行修改以添加 resources 目录(与项目 srcclassesdist 目录)。将 fxmlcss 放在 resources 目录中,以便将它们包含在 jar 中。复制文件的目录结构将与资源目录的目录结构匹配,所以如果你只是将它们放在没有子目录的资源目录中,文件将显示在 jar 文件的根目录中(所以当你使用资源相对于根引用它们(例如 FXMLLoader.load(getResource("/main.fxml")))。我在没有测试的情况下进行了这些修改,因为我不再使用 ant 进行构建。

<property name="build.src.dir" value="src"/>
<property name="build.resources.dir" value="resources"/>
<property name="build.classes.dir" value="classes"/>
<property name="build.dist.dir" value="dist"/>

<target name="default" depends="clean,compile">

<taskdef resource="com/sun/javafx/tools/ant/antlib.xml"
  uri="javafx:com.sun.javafx.tools.ant"
  classpath="${JAVA_HOME}/lib/ant-javafx.jar"/>

  <fx:application id="HelloWorldID"
    name="JavaFXHelloWorldApp"
    mainClass="HelloWorld"/>

  <fx:resources id="appRes">
    <fx:fileset dir="${build.dist.dir}" includes="HelloWorld.jar"/>
  </fx:resources>

  <fx:jar destfile="${build.dist.dir}/HelloWorld.jar">
    <fx:application refid="HelloWorldID"/>
    <fx:resources refid="appRes"/>
    <fileset dir="${build.classes.dir}"/>
    <fileset dir="${build.resources.dir}"/>
  </fx:jar>

  . . .

</target>

您可能有 运行时间问题而不是构建问题

似乎应用程序构建正常,但您在尝试 运行 您的应用程序时遇到 运行 时间错误,这可能是由于您的应用程序代码中的问题或因为所需的资源执行不存在。

尝试不包含任何 FXML 的 simpler application 并构建并执行它 - 如果可行,那么您的错误要么在您的应用程序代码中,要么在复制 FXML 资源到你的构建包。

关于 JavaFX Ant 任务的 Intellij 语法高亮

关于 Intellij 中的 "URI is not registered" 错误,这有点转移注意力。这只是意味着您尚未使用 Idea 注册 fx 命名空间的模式,因此 Idea 无法验证文档(并在 XML 标记上提供上下文相关代码完成)。只要您没有在 XML 中出现语法或结构错误(您可能没有,或者 ant 可能会拒绝它),那么您可以根据需要忽略此类错误消息。

您可以在此处找到更多相关信息:

注意: 我不认为 Oracle 为 JavaFX ant 任务提供了完整的 XML 模式,所以它可能是不可能的为您配置 Idea 以验证 ant build.xml 文件的 JavaFX ant 任务元素。但是,这不应阻止您构建应用程序 - 您最好的策略可能是将 Idea 配置为 ignore the JavaFX ant tasks XML schema,这样它就不会再在 build.xml 文件上显示烦人且误导的红色高亮显示。

替代技术

您可能(或可能不会)发现使用 JavaFX Maven plugin or JavaFX Gradle plugin 比直接使用 JavaFX Ant Tasks 更适合您。