0

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.

Neilos
  • 2,696
  • 4
  • 25
  • 51

2 Answers2

4

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.

Community
  • 1
  • 1
jenaiz
  • 547
  • 2
  • 15
  • @jenaiz I'm not sure I get what you mean. All the .class files exist in the build directory in the places they should. I can't see what you are doing differently to me. – Neilos Jan 02 '12 at 15:11
  • If you compile without no problems and your classes are in the **{build}** folder, then the problem should be in the code when you package your jar. Because there you are saying what are your basedir (directory from where to jar), what libraries your are including (in this case no libraries included) ... and you are creating a manifest with a Main class called: nat.rutherford.DesktopStarter – jenaiz Jan 02 '12 at 15:21
  • @jenaiz forgive my ignorance but if the problem is there then I have no idea how to fix it, could you give me a push in the right direction please? – Neilos Jan 02 '12 at 15:42
  • @Neilos The problem is the Class that you are using as Main-Class when you package your jar, it doesn't work. I try to explain where is the problem in your build.xml. I hope that help you. – jenaiz Jan 02 '12 at 16:55
0

It looks like you've added a manifest to your executable JAR that spells out nat.rutherford.DesktopStarter as your main class.

I'd recommend that you open the JAR and verify that the manifest.mf appears and does indeed say what your Ant build.xml does.

I'd also verify that your DesktopStarted.class appears in a folder path nat.rutherford. If it doesn't, the JVM won't find it.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • I checked the manifest using a test class to read it from the JAR, it prints the main attributes, the output is; - Ant-Version: Apache Ant 1.7.1 - Manifest-Version: 1.0 - Created-By: 20.4-b02 (Sun Microsystems Inc.) - Main-Class: nat.rutherford.DesktopStarter – Neilos Jan 02 '12 at 15:03
  • The the JAR doesn't contain a file named DesktopStart.class in path nat/rutherford – duffymo Jan 02 '12 at 15:25
  • Yes this does seem to be the case but I don't know what I am doing wrong for it not to be there. As far as I can tell it should be there?!? The .class files exist in the folder 'build', this is where I JAR from and I create the manifest correctly, so what am I missing? This is my first ever build so it may be something elementary I'm not seeing. – Neilos Jan 02 '12 at 15:40
  • Jenaiz told you the answer - your Ant build.xml is incorrect. – duffymo Jan 02 '12 at 15:54
  • Then there is clearly something I am missing because I'm just not seeing it lol. Thanks for your help though. – Neilos Jan 02 '12 at 16:19
  • I'd recommend that you leave Eclipse out of it. Try running your Ant build.xml in a command shell and see what it gives you. It should be easy to fix. – duffymo Jan 02 '12 at 16:39