1
votes

I am attempting to use Ant to package my Java application as a self-contained application. However, when I attempt to load the JavaFX Ant tasks from ant-javafx.jar using taskdef, I get a 'failed to create task or type' error. My dist target is as follows (the jar target and all of its dependencies execute without error):

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

    <fx:deploy outdir="${antbuild}/packager" outfile="InventoryManager" nativeBundles="exe">

        <fx:application name="Inventory Manager" mainClass="inventorymanager.RunInventoryManager" version="1.0"/>

        <fx:resources>
            <fx:fileset dir="${dist}" includes="InventoryManager-${DSTAMP}.jar"/>
        </fx:resources>

        <fx:info title="Inventory Manager" description="Inventory manager for a store.">
            <fx:association extension="inv" description="Inventory File"/>
        </fx:info>

        <fx:bundleArgument arg="win.menuGroup" value="Inventory Manager"/>

    </fx:deploy>
</target>

When I run this target, I get the following error:

BUILD FAILED
C:\Users\John\eclipse-workspace\InventoryManager\src\inventorymanager\build.xml:45: Problem: failed to create task or type javafx:com.sum.javafx.tools.ant:deploy
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
No types or tasks have been defined in this namespace yet

<echo>${env.JAVA_HOME}/lib/ant-javafx.jar</echo> outs C:\Program Files\Java\jdk1.8.0_151/lib/ant-javafx.jar, so I know that JAVA_HOME is pointing to the correct location.

Does the fact that the path contains a mix of forward and backslashes create an issue? If not, what else might I try?

In case this has any bearing on the problem, I am invoking Ant from inside Eclipse Oxygen on Windows 10.

UPDATE: I tried using the solution found here. This was unsuccessful, so I tried changing ${env.JAVA_HOME} back to ${java.home} so that they were both pointing to the same place <echo>${java.home}/lib/ant-javafx.jar</echo> outs C:\Program Files\Java\jdk1.8.0_151\jre/lib/ant-javafx.jar, which is not the correct path to the JAR file. Is there a way to get to C:\Program Files\Java\jdk1.8.0_151\ without resorting to using an environment variable?

Now that I've figured out how to turn on verbose, the verbose output from the dist task is as follows:

parsing buildfile jar:file:/C:/Program%20Files/Java/jdk1.8.0_151/lib/ant-javafx.jar!/com/sun/javafx/tools/ant/antlib.xml with URI = jar:file:/C:/Program%20Files/Java/jdk1.8.0_151/lib/ant-javafx.jar!/com/sun/javafx/tools/ant/antlib.xml from a zip file
 [macrodef] creating macro  javafx:com.sun.javafx.tools.ant:init-ant

Full example as requested:

C:\Users\John\eclipse-workspace\HelloWorld\src\helloworld\HelloWorld.java contains:

package helloworld;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

C:\Users\John\eclipse-workspace\HelloWorld\src\helloworld\build.xml contains:

<?xml version="1.0" encoding="UTF-8"?>
<project name="Hello World" default="dist" basedir="C:/Users/John/eclipse-workspace/HelloWorld" xmlns:fx="javafx:com.sum.javafx.tools.ant">
    <description>
            Ant build of a self-contained Hello World application.
    </description>

    <!-- Set directory properties for this build -->
    <property name="antbuild" location="antbuild"/>
    <property name="src" location="src"/>
    <property name="dist" location="dist"/>
    <property environment="env"/>

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

    <target name="compile" depends="init" description="Compiles the source code.">
        <javac includeantruntime="false" srcdir="${src}" destdir="${antbuild}"/>
    </target>

    <target name="jar" depends="compile" description="Creates the JAR file for the application.">

        <manifest file="${antbuild}/MANIFEST.MF">
            <attribute name="Main-Class" value="helloworld.HelloWorld"/>
        </manifest>

        <jar manifest="${antbuild}/MANIFEST.MF" jarfile="${dist}/HelloWorld-${DSTAMP}.jar" basedir="${antbuild}"/>
    </target>

    <echo>${env.JAVA_HOME}/lib/ant-javafx.jar</echo>
    <target name="dist" depends="jar">
        <taskdef resource="com/sun/javafx/tools/ant/antlib.xml"
                uri="javafx:com.sun.javafx.tools.ant"
                classpath="${env.JAVA_HOME}/lib/ant-javafx.jar;src"/>

        <fx:deploy outdir="${antbuild}/packager" outfile="HelloWorld" nativeBundles="exe">

            <fx:application name="Hello World" mainClass="helloworld.HelloWorld" version="1.0"/>

            <fx:resources>
                <fx:fileset dir="${dist}" includes="HelloWorld-${DSTAMP}.jar"/>
            </fx:resources>

            <fx:info title="Hello World" description="Hello World program."/>

            <fx:bundleArgument arg="win.menuGroup" value="Hello World"/>

        </fx:deploy>
    </target>

    <target name="cleanup">
        <!-- Deletes the antbuild directory -->
        <delete dir="${antbuild}"/>
        <delete dir="${dist}"/>
    </target>

</project>

Attempting to run the dist target of build.xml yields the following (non-verbose) output:

Buildfile: C:\Users\John\eclipse-workspace\HelloWorld\src\helloworld\build.xml
        [echo] C:\Program Files\Java\jdk1.8.0_151/lib/ant-javafx.jar

init:
       [mkdir] Created dir: C:\Users\John\eclipse-workspace\HelloWorld\antbuild

compile:
       [javac] Compiling 1 source file to C:\Users\John\eclipse-workspace\HelloWorld\antbuild

jar:
         [jar] Building jar: C:\Users\John\eclipse-workspace\HelloWorld\dist\HelloWorld-20171121.jar

dist:

BUILD FAILED
C:\Users\John\eclipse-workspace\HelloWorld\src\helloworld\build.xml:36: 
Problem: failed to create task or type javafx:com.sum.javafx.tools.ant:deploy
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
No types or tasks have been defined in this namespace yet


Total time: 1 second
1
Yes, xmlns:fx="javafx:com.sum.javafx.tools.ant" is in my <project> element.train1855
As an alternative to ant, you may wish to try either the JavaFX Maven Plugin or the JavaFX Gradle Plugin. I don't use ant natively much, so I can't offer much advice on how to get your app working with ant other than to very closely compare your ant file with the ant file for sample projects provided by Oracle and, if you still can't work things out, publishing an minimal reproducible example to StackOverflow rather than partial file snippets.jewelsea
Try Eclipse > YOUR PROJECT > build.xml > External tools configuration > JRE Tab > per my update below.paulsm4
@jewelsea What else do you need me to add to make this an MCVE? The other tasks in the build.xml file aren't contributing to the error, nor is any of the source code I am trying to build.train1855
The description of an minimal reproducible example is pretty self explanatory. You need something that somebody could just copy and paste and execute to replicate the issue without any additions or changes. If the source is not an issue (and it's not because this is a build issue), then just supply source for a hello world app. You need to supply the complete ant file you are using. If some tasks aren't related to the error, then delete them, but the ant file should still be whole so that a copy and paste will allow reproduction, otherwise it is just guesswork to guess what may be wrong.jewelsea

1 Answers

0
votes

My first question: Q: do you have ant-javafx.jar in jdk_home/lib? The answer appears to be "Yes".

Q: Do you have xmlns:fx="javafx:com.sun.javafx.tools.ant" in your <project> root element?

Q: What exactly is on Line 45?

Also: please be sure to read the Oracle documentation:

https://docs.oracle.com/javase/8/docs/technotes/guides/deploy/javafx_ant_tasks.html

ADDENDUM:

Please look at the suggestions here (your exact error, and several possible workarounds): JAVAFx Build Failed

UPDATE: Based on your update, it sounds like you want to set the JRE for your Ant build:

Eclipse > YOUR PROJECT > build.xml > External tools configuration > JRE Tab > <= Select the JRE you want

It's important to note that the Windows variable "JAVA_HOME" is NOT used directly by either Eclipse or Ant.

It's also important to note that the JRE you use for Ant builds can be (and often is) different from the JRE you use for your project.