Search code examples
javajavafxant

bad name in value for --add-modules when trying to compile through ant


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.


Solution

  • 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"/>