views:

32

answers:

2

I am trying to work through the HelloWorld example on the Web that shows you how to create a build file using ANT in Eclipse. This is the project build file from the web example

<?xml version="1.0" encoding="UTF-8"?>
<project name="HW.makejar" default="makejar" basedir=".">
    <target name="makejar" description="Create a jar for the HW project">
        <jar jarfile="HelloWorld.jar" includes="*.class" basedir="."/>
    </target>
</project>

But when I run the resulting jar, I get this error message failed to load Main-Class manifest attribute from HelloWorld.jar.

So then I tried it like this:

<?xml version="1.0" encoding="UTF-8"?>
<project name="HW.makejar" default="makejar" basedir=".">
    <target name="makejar" description="Create a jar for the HW project">
        <jar jarfile="HelloWorld.jar" includes="*.class" basedir=".">
            <manifest>
                <attribute name="Main-Class" value="ami.HelloWorld" />
            </manifest>
        </jar>

    </target>
</project>

When I reran the resulting jar, I got the following error message:

Exception in thread "main" java.lang.NoClassDefFoundError: ami/HelloWorld

What am I doing wrong. By the way, when I manually compile the source and specify the Main.class within Eclipse, the resulting jar runs perfectly.

+1  A: 

Does your jar file contain any class files? If not, try:

<jar jarfile="HelloWorld.jar" includes="**/*.class" basedir=".">
richj
As you suggested, the jar file did not contain any class files. I changed the project file as you suggested. Now my project file looks like this:<?xml version="1.0" encoding="UTF-8"?><project name="HW.makejar" default="makejar" basedir="."> <target name="makejar" description="Create a jar for the HW project"> <jar jarfile="HelloWorld.jar" includes="**\*.class" basedir="."> </jar> </target></project>The Class file is in there but this is what happens when I run it.c:\workspace\HW>java -jar HelloWorld.jarFailed to load Main-Class manifest attribute fromHelloWorld.jar
Elliott
Try adding the <manifest> tag back into the project file - I think the error message is saying that the Main-Class attribute is missing from the manifest.
richj
I just reconstructed the project with all these changes and now it works. Who knows. Maybe something got corrupted. Anyway, thanks for the pointers It got me started in the right direction.
Elliott
A: 

Is your basedir correct? If you jar tvf HelloWorld.jar is the class HelloWorld.class listed under the ami folder? If it is listed directly under the root you need to add it from its parent folder, not from the ami folder.

rsp