0

I am trying to include the javafx.controls module in my project and compile it through ant, rather then use javaFX javac --module-path "..\lib\javafx-sdk-17.0.2\lib" --add-modules javafx.controls App.java

java --module-path "..\lib\javafx-sdk-17.0.2\lib" --add-modules javafx.controls App commands to compile the file(s).

This is how my ant build.xml looks like right now:

<project name="projectName" default="dist">
    <property file="build.properties"/>
    <property file="${user.home}/build.properties"/>

    <path id="run.classpath">
        <fileset dir="${dist.dir}">
            <include name="${project.name}.jar"/>
        </fileset>
    </path>

    <target name="compile">
        <mkdir dir="${build.dir}"/>
        <javac srcdir="${src.dir}"
               destdir="${build.dir}"
               debug="${compile.debug}"
               deprecation="${compile.deprecation}"
               optimize="${compile.optimize}"
               includeantruntime="false">
                <classpath>
                    <fileset dir="${lib.dir}/javafx-sdk-17.0.2/lib">
                        <include name="**/*.jar" />
                    </fileset>
                </classpath>
                <compilerarg line="--add-modules ${lib.dir}/javafx-sdk-17.0.2/lib/javafx.controls"/>
        </javac>
    </target>

    <target name="dist" depends="compile">
        <mkdir dir="${dist.dir}"/>
        <jar jarfile="${dist.dir}/${project.name}.jar"
             basedir="${build.dir}"
             manifest="${src.dir}/Manifest.mf">
             <zipgroupfileset dir="${lib.dir}/javafx-sdk-17.0.2/lib" includes="**/*.jar"/>
        </jar>
    </target>

    <target name="build" depends="dist">
        <java jar="${dist.dir}/${project.name}.jar" fork="true">
        </java>
    </target>

    <target name="clean">
        <delete dir="${build.dir}"/>
        <delete dir="${dist.dir}"/>
    </target>
</project>

The error I keep on getting is: error: bad name in value for --add-modules option: 'C:\xampp\htdocs\projectName/lib/javafx-sdk-17.0.2/lib/javafx.controls'. I have looked up the error, but the answers are very vague in my opinion and have not helped me get more knowledge in regards to the error.

  • I am not an expert on the topic but I notice the path in your error message uses '\' and '/'. May this be part of the problem? – Rosso Feb 15 '22 at 19:32
  • @Rosso I have no idea. I don't think so, because I tried to with the backward slashes as well, but the error remains the same. – I try so hard but I cry harder Feb 15 '22 at 22:02
  • 1
    I recommend switching to maven or gradle. – jewelsea Feb 16 '22 at 00:09
  • Your compile issues aside, you are going to have problems trying to create a distribution jar which includes JavaFX libraries from the sdk as jar files (which don’t include native portions) you need to instead separately download the jmods and link them with jlink or jpackage. I have no instructions on how to do that with ant. – jewelsea Feb 16 '22 at 03:39

1 Answers1

2

What you are doing wrong

You are providing an incorrect value to --add-modules.

You are not providing a string to ant that matches the example command in your question.

In your question, for the example javac command, you have:

--module-path "..\lib\javafx-sdk-17.0.2\lib" --add-modules javafx.controls

but in your ant script you have

--add-modules ${lib.dir}/javafx-sdk-17.0.2/lib/javafx.controls

You have tried to compress the --module-path argument with the --add-modules argument. Those arguments must remain separate and have different values.

ant javac support for the Java Platform Module System

The ant javac task has a few parameters for working with the Java module system, you should use appropriate ones:

  • modulepath

    Use this to reference the directory set containing your JavaFX modules (e.g. the SDK lib directory).

Do not put the JavaFX libraries on the class path, they are modules and belong on the module path.

Specific changes required for your ant script

I am not familiar enough with the ant argument syntax to provide the exact ant XML elements for the correct definitions of the arguments for your case.

If somebody knows the exact values required in ant element syntax, they can edit this answer and put the values here.

Background info

Read the javac man page if you want to understand what the --module-path and --add-modules arguments are, what data should be supplied to them, and the format required for that data.

Also review understanding java 9 modules.

Alternate approach: defining module info

As an alternative to adding modules via the --add-modules VM argument, you can instead define a module-info.java file in your application to make it modular and require the modules there.

This will work fine if you aren't depending on non-modular code.

Even if you define a module-info.java file, you still need to set the --module-path for the compiler and runtime so that it can find the module implementations. Build tools such as Maven will do this automatically for defined dependencies.

However, ant isn't aware of build dependencies and needs manual help with parameters to support the module system. You will need to set the module path manually via modulepath arguments to the compiler. Similarly, you will need to set VM arguments for runtime module support if you have a java execution task in ant.

solution

<target name="compile">
    <mkdir dir="${build.dir}"/>
    <javac srcdir="${src.dir}"
           destdir="${build.dir}"
           debug="${compile.debug}"
           deprecation="${compile.deprecation}"
           optimize="${compile.optimize}"
           includeantruntime="false">
            <classpath>
                <fileset dir="${lib.dir}/javafx-sdk-17.0.2/lib">
                    <include name="**/*.jar" />
                </fileset>
            </classpath>
            <compilerarg line="--module-path ${lib.dir}/javafx-sdk-17.0.2/lib/"/>
            <compilerarg line="--add-modules javafx.controls"/>
    </javac>
</target>

<target name="build" depends="dist">
    <java jar="${dist.dir}/${project.name}.jar" fork="true">
        <jvmarg line="--module-path ${lib.dir}/javafx-sdk-17.0.2/lib/"/>
        <jvmarg line="--add-modules javafx.controls"/>
    </java>
</target>

First use the compilerarg with the line, to indicate what you want to compile during the javac process.

<compilerarg line="--module-path ${lib.dir}/javafx-sdk-17.0.2/lib/"/>
<compilerarg line="--add-modules javafx.controls"/>

You then provide these lines within the java process as well.

<jvmarg line="--module-path ${lib.dir}/javafx-sdk-17.0.2/lib/"/>
<jvmarg line="--add-modules javafx.controls"/>
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • So my best bet is implementing maven? – I try so hard but I cry harder Feb 16 '22 at 15:22
  • I often try to avoid opinionated answers for questions like this, but IMO, yes, for most projects, I think it is better to use Maven for this kind of work, and for some projects Gradle. I would only consider using ant for a legacy project for which it is difficult to upgrade the build system, but not for new projects. That said, ant is still a capable build tool and can accomplish a lot of tasks if you put the appropriate amount of effort and skill into finding solutions. – jewelsea Feb 18 '22 at 09:47
  • Often tool support for Maven based projects is better IMO. For instance, configuring a new JavaFX project like this from scratch with ant and setting it up to work with an IDE would probably take a few hours to get right even if you know what you are doing. But if you use the [new JavaFX project wizard in Idea](https://www.jetbrains.com/help/idea/javafx.html) to create a new Maven JavaFX project, even a relative beginner can have something working And be productive in a couple of minutes. – jewelsea Feb 18 '22 at 09:57