Java Information Center




Get DrJavaThis is the latest stable release of DrJava. DrJava is now a Sourceforge project. This site has a complete tutorial on installing Java on your machine.

For Newbies

General References

A Basic Ant File for Building Jar Files

Copy this file and place it in a file named build.xml.

<?xml version = "1.0" ?>
<project default = "main">
    <target name = "main" depends = "compile, compress">
    <echo>
        Building the .jar file
    </echo>
    </target>

    <target name = "compile">
        <javac srcdir = "."/>
    </target>

    <target name = "compress">
        <jar jarfile = "Foo.jar" basedir = "." includes = "*.class">
            <manifest>
                <attribute name = "Main-Class" value = "Foo"/>
            </manifest>
        </jar>
    </target>

</project>

Now place this in a directory along with your .java file. The file here is named Foo.java; its contents are as follows.

public class Foo
{
    public static void main(String[] args)
    {
        System.out.println("Foo");
    }
}

Now enter the ant command at the command line like so. You will need to have ant installed.

$ ant

Then you will see this

$ ant
Buildfile: /Users/morrison/antText/build.xml

compile:
    [javac] /Users/morrison/antText/build.xml:10: warning: 'includeantruntime'
was not set, defaulting to build.sysclasspath=last; set to false for repeatable
builds
    [javac] Compiling 1 source file

compress:
      [jar] Building jar: /Users/morrison/antText/Foo.jar

main:
     [echo] 
     [echo]         Building the .jar file
     [echo]     

BUILD SUCCESSFUL
Total time: 1 second

To run the jar file, just type

$ java -jar Foo.jar
Foo