Cannot find Main Class in File Compiled With Ant

Looks like your runtime classpath is missing the jar containing the class org.supercsv.io.ICsvBeanReader.

The gotcha is that you cannot set the classpath from the command-line when calling an executable jar. You have to set it within the manifest as follows:

<target name="dist" depends="compile" description="Generates distributable">
    <!-- creates the distribution directory -->
    <mkdir dir="${dist}/lib" />

    <!-- Remove manifest. This jar will end up on the classpath of CC.jar -->
    <jar jarfile="${dist}/lib/CC-${DSTAMP}.jar" basedir="${build}"/>

    <!-- Fancy task that takes the pain out creating properly formatted manifest value -->
    <manifestclasspath property="mf.classpath" jarfile="${dist}/lib/CC.jar">
        <classpath>
            <fileset dir="${dist}/lib" includes="*.jar"/>
        </classpath><!--end tag-->
    </manifestclasspath>

    <!-- This is the executable jar -->
    <jar jarfile="${dist}/lib/CC.jar" basedir="${build}">
        <manifest>
            <attribute name="Main-Class" value="jab.jm.main.Test"/>
            <attribute name="Class-Path" value="${mf.classpath}"/> 
        </manifest>
    </jar>

</target>

This approach will allow you to run the jar as follows:

java -jar CC.jar

Without the extra manifest entry you have to run the jar as follows:

java -cp CC.jar:CC-DSTAMPVALUE.jar jab.jm.main.Test

Note

Only the CC.jar is executable and needs the special manifest. Using this pattern means future additional jars, placed into the lib directory, will be automatically included in the run-time classpath. (Useful for open source dependencies like log4j)

Obviously, when running the CC.jar you’ll get a similar error if the jar files are not present 🙂

Leave a Comment