Java – Ant build (Eclipse) – Could not find the main class: nat.rutherford.DesktopStarter

antbuild.xmleclipsejava

I am having a little trouble with my first ever ant build in eclipse, here is my build.xml build file.

<project name="Rutherford" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
    <!-- set global properties for this build -->
    <property name="src" location="src"/>
    <property name="build" location="build"/>
    <property name="dist"  location="dist"/>
    <property name="libs" value="libs"/>

    <path id="classpath">
        <fileset dir="${libs}" includes="**/*.jar"/>
    </path>

    <target name="init">
        <!-- Create the time stamp -->
        <tstamp/>
        <!-- Create the build directory structure used by compile -->
        <mkdir dir="${build}"/>
    </target>

    <target name="compile" depends="init"
        description="compile the source " >
        <!-- Compile the java code from ${src} into ${build} -->
        <javac srcdir="${src}" destdir="${build}" classpathref="classpath">
            <compilerarg line="-encoding utf-8"/>
        </javac>
    </target>

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

        <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
        <jar jarfile="${dist}/MyProject-${DSTAMP}.jar" basedir="${build}">
            <manifest>
                <attribute name="Main-Class" value="nat.rutherford.DesktopStarter"/>
            </manifest>
        </jar>
    </target>

    <target name="run">
        <java jar="${dist}/MyProject-${DSTAMP}.jar" fork="true"/>
    </target>

    <target name="clean"
        description="clean up" >
        <!-- Delete the ${build} and ${dist} directory trees -->
        <delete dir="${build}"/>
        <delete dir="${dist}"/>
    </target>
</project>

It compiles ok with no warnings or errors, but when I try to run the .jar it says 'Could not find the main class: nat.rutherford.DesktopStarter. Program will now exit' =(

I have read a ton of pages on the matter but so far nothing conclusive.

I was able to compile it using Eclipse -> File -> Export ->Java -> Runnable Jar File. But I use some UTF-8 encoded .txt files that it seems not to be able to deal with that way and I need them! ie I have greek characters that should read…dσ/dΩ… but currently read… dÃ/d©… which isn't going to work ^^

So basically I need to make my Ant build work, baring in mind that it needs to be able to handle my UTF-8 encoded .txt files too.

Best Answer

The problem is in your task dist when you create your jar. If your compilation is right and there is no problem when you package your jar. Things that are wrong:

  • <mkdir dir="${dist}/lib"/> -> this don't have mean, you don't use it never

  • second you are not including your libraries in your jar, then when you try to execute your jar it doesn't work that why you are seeing the error message Could not find the main class: nat.rutherford.DesktopStarter. Program will now exit You could see that your libraries aren't within your jar using Winzip or similar. I suppose that you are seeing your problem when you try to execute the jar directly using windows or similar. A good way to see what is happening, seeing the problem printed in the console is executing your jar in the next way: java -jar MyProject-20120102.jar

  • See: How to include your libraries in you jar?

  • And if you want to know more about jar packaging using ant try this.

  • Another thing that you need to modify the Class-path attribute in your manifest to include the libraries within your ${libs} folder.

Related Topic