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
xmlns:fx="javafx:com.sum.javafx.tools.ant"
is in my<project>
element. – train1855Eclipse > YOUR PROJECT > build.xml > External tools configuration > JRE Tab >
per my update below. – paulsm4