3.7. Building a Java Program

Warning

Note that abuild's Java support is considered alpha as of version 1.0. Version 1.1 of abuild may include a Java solution that is not backward compatible with the solution in version 1.0.

In Java, there is no deep distinction between a “library” and a “program” except that a JAR file that provides a program must have a main method. If a JAR file contains a main method, it can be executed, though it can also be used as a library. A JAR file can also contain a manifest file that identifies a class that contains a main method. Starting with version 1.0.1, abuild adds the Main-Class attribute to the manifest file when the abuild.main-class property is set in the Abuild-ant.properties.

Here are the relevant files for the program example:

basic/java-program/src/java/com/example/basic/BasicProgram.java

package com.example.basic;

import com.example.basic.BasicLibrary;

public class BasicProgram
{
    public static void main(String[] args)
    {
        BasicLibrary l = new BasicLibrary(10);
        l.hello();
    }
};

basic/java-program/Abuild.conf

this: java-program
platform-types: java
parent-dir: ..
deps: java-library

basic/java-program/Abuild-ant.properties

abuild.jar-name = java-program.jar
abuild.main-class = com.example.basic.BasicProgram
abuild.wrapper-name = java-program

Note the addition of the abuild.main-class and abuild.wrapper-name properties. If these are both set, abuild will create a wrapper executable (script file and/or batch file as appropriate) so that you can run the resulting program with the correct classpath. The wrapper programs are placed directly in the abuild output directory.

Here is the output of running abuild --with-deps in this directory. As in the C++ program example, the output has been modified slightly: in addition to the --topdir-- substitution, we have also filtered out time stamps and other strings that could potentially differ between platforms:

example.basic-java-program.out

abuild: java-library (abuild-java): all
Buildfile: --abuild.xml--

init:
    [mkdir] Created dir: --topdir--/basic/java-library/abuild-java/empty
    [mkdir] Created dir: --topdir--/basic/java-library/abuild-java/classes
    [mkdir] Created dir: --topdir--/basic/java-library/abuild-java/dist
    [mkdir] Created dir: --topdir--/basic/java-library/abuild-java/src/java
    [mkdir] Created dir: --topdir--/basic/java-library/abuild-java/src/r\
\esources
    [mkdir] Created dir: --topdir--/basic/java-library/abuild-java/src/web
    [mkdir] Created dir: --topdir--/basic/java-library/abuild-java/src/conf

-do-compile:
    [javac] Compiling 1 source file to --topdir--/basic/java-library/abu\
\ild-java/classes

-jar-without-main-class:
      [jar] Building jar: --topdir--/basic/java-library/abuild-java/dist\
\/java-library.jar

BUILD SUCCESSFUL
Total time: <time>
abuild: java-program (abuild-java): all
Buildfile: --abuild.xml--

init:
    [mkdir] Created dir: --topdir--/basic/java-program/abuild-java/empty
    [mkdir] Created dir: --topdir--/basic/java-program/abuild-java/classes
    [mkdir] Created dir: --topdir--/basic/java-program/abuild-java/dist
    [mkdir] Created dir: --topdir--/basic/java-program/abuild-java/src/java
    [mkdir] Created dir: --topdir--/basic/java-program/abuild-java/src/r\
\esources
    [mkdir] Created dir: --topdir--/basic/java-program/abuild-java/src/web
    [mkdir] Created dir: --topdir--/basic/java-program/abuild-java/src/conf

-do-compile:
    [javac] Compiling 1 source file to --topdir--/basic/java-program/abu\
\ild-java/classes

-jar-with-main-class:
      [jar] Building jar: --topdir--/basic/java-program/abuild-java/dist\
\/java-program.jar

BUILD SUCCESSFUL
Total time: <time>