Search code examples
javajsonxmlant

Ant fails to build org.json, org.json does work


I have tested whether org.json was correctly installed through a very basic project (literally one class and that was it), so I know the problem must be when I am trying to build my project with ant.

I could only find one question which was identical to my problem, but it did not solve it.

This is how I installed org.json https://www.tutorialspoint.com/org_json/org_json_environment.htm

How my build.xml looks like:

<project name="Tracer" 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}"/>
        <mkdir dir="${build.dir}/assets"/>
        <javac srcdir="${src.dir}/nhl"
               destdir="${build.dir}"
               debug="${compile.debug}"
               deprecation="${compile.deprecation}"
               optimize="${compile.optimize}">
            <classpath>
                <pathelement path="C:/JSON/json-20210307.jar"/><!-- tried an absolute path to my jar -->
            </classpath>
        </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"/>
    </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 get:

 [java] Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONTokener
 [java]     at (...).main(Unknown Source)
 [java] Caused by: java.lang.ClassNotFoundException: org.json.JSONTokener
 [java]     at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)
 [java]     at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)
 [java]     at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)

I am using git-bash when using ant if that matters.

I have also done the reference libraries part.


Solution

  • <project name="RayTracer" default="dist">
        <property file="build.properties"/>
        <property file="${user.home}/build.properties"/>
        <property name="prop1" value="Property 1 from Buildfile"/>// in case you want to set default system properties
        <property name="prop2" value="Property 2 from Buildfile"/>
    
    <path id="run.classpath">
        <fileset dir="${dist.dir}">
            <include name="${project.name}.jar"/>
        </fileset>
    </path>
    
    <target name="compile">
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${build.dir}/assets"/>
        <javac srcdir="${src.dir}/nhl"
               destdir="${build.dir}"
               debug="${compile.debug}"
               deprecation="${compile.deprecation}"
               optimize="${compile.optimize}"
               includeantruntime="false">
                <classpath>// compile all JAR files in the src/libs/JSON directory
                    <fileset dir="${src.dir}/libs/JSON">
                        <include name="**/*.jar" />
                    </fileset>
                </classpath>
        </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="${src.dir}/libs/JSON" includes="**/*.jar"/>// use the file
        </jar>
    </target>
    
    <target name="build" depends="dist">
        <java jar="${dist.dir}/${project.name}.jar" fork="true">
            <!-- <sysproperty key="prop1" value="${prop1}"/> --> //set system property when providing argument
            <arg value="${arg0}"/> // provide arguments without using system properties
            <arg value="${arg1}"/>
        </java>
    </target>
    
    <target name="clean">
        <delete dir="${bin.dir}"/>
        <delete dir="${build.dir}"/>
        <delete dir="${dist.dir}"/>
        <delete file="output.bmp"/>
    </target>