user4671628
user4671628

Reputation:

Cannot build JavaFX application using Ant

I need to create build file for ant to build my JavaFX project, I have searched a lot, but nothing helped me. It still show errors but compiles. When I tries to run jar file - exceptions. I have tried different paths, but still nothing.
Here is my 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>

** is fully "red" in my IDEA (Intellij IDEA.
* is underlined .

But nevertheless it builds using ant -f build.xml. But when I tries to run jar file I am getting next exceptions. Exception in Application start method

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)

Upvotes: 3

Views: 9824

Answers (1)

jewelsea
jewelsea

Reputation: 159321

Update on copying resources using JavaFX ant tasks

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 ?

Based on the Java Deployment Tutorial JavaFX Ant Tasks HelloWorld build.xml sample, and modifying it to add a resources directory (which is a sibling to the project src, classes and dist directories). Place your fxml and css in the resources directory to get them included in the jar. The directory structure of the copied files will match the directory structure of the resources directory, so if you just put them in the resources directory with no sub-directories, the files will show up in the root of the jar file (so when you use the resources reference them relative to the root (e.g. FXMLLoader.load(getResource("/main.fxml"))). I made these modifications without testing as I don't use ant for builds anymore.

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

You likely have a runtime issue not a build issue

It would seem that the application builds fine and you are getting a runtime error trying to run your application, either due to an issue in your application code or because the resources required for execution are not present.

Try a simpler application which does not include any FXML and build and execute that - if that works than either your error was in your application code or in the code which copies the FXML resources to your build package.

On Intellij syntax highlighting of JavaFX Ant tasks

Regarding the "URI is not registered" error in Intellij, that is a bit of a red-herring. It just means that you haven't registered the schema for the fx namespace with Idea, so Idea cannot validate the document (and provide context sensitive code completion on XML tags). As long as you haven't made syntax or structure errors in your XML (which you probably haven't or ant would likely reject it), then you can ignore such error messages if you wish.

You can find more information on this here:

Note: I don't think Oracle provide a full XML schema for the JavaFX ant tasks, so it will probably not be possible for you to configure Idea to validate the JavaFX ant task elements of your ant build.xml file. However, that should not prevent you from building your application - your best policy is probably to configure Idea to ignore the JavaFX ant tasks XML schema, so it no longer displays annoying and misdirecting red highlights on your build.xml file.

Alternative Technology

You may (or may not) find using the JavaFX Maven plugin or JavaFX Gradle plugin a better solution for you than using the JavaFX Ant Tasks directly.

Upvotes: 2

Related Questions