1

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.

5
  • <!-- <?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> --> This is the first XML file Commented Aug 1, 2010 at 16:54
  • <!-- <?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> --> This is the second XML file Commented Aug 1, 2010 at 16:55
  • All this xml in comments is not very readable, can you delete those comments and edit the question to include the xml you have so far? Commented Aug 1, 2010 at 19:00
  • Now the project file is: <?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="HelloWorld" /> </manifest> </jar> </target> </project> But when I run it, I'm back to: c:\workspace\HW>java -jar HelloWorld.jar Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld In the jar: I see both the class file and the manifiest. I'll post below Commented Aug 1, 2010 at 23:11
  • Here is the manifest: Manifest-Version: 1.0 Ant-Version: Apache Ant 1.7.1 Created-By: 16.3-b01 (Sun Microsystems Inc.) Main-Class: HelloWorld Commented Aug 1, 2010 at 23:11

2 Answers 2

2

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

<jar jarfile="HelloWorld.jar" includes="**/*.class" basedir=".">
Sign up to request clarification or add additional context in comments.

3 Comments

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.jar Failed to load Main-Class manifest attribute from HelloWorld.jar
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.
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.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.