Hello World Application Project


The goal of the Hello World application project is to create a standalone Java application that uses greetmodel.jar to get the greeting message. The application project buildfile is nearly identical to the model project buildfile, so we focus our discussion on the differences between the two buildfiles . We also explain how to make a JAR file an executable JAR file.

Overview of Application Java Classes

The Java source code for this application is as simple as it gets for the Hello World Model 2 examples. Here is the Java application:

 package xptoolkit; import xptoolkit.model.GreetingFactory; import xptoolkit.model.Greeting; public class HelloWorld{     public static void main(String []args)throws Exception{        Greeting greet = (Greeting)                GreetingFactory.getGreetingFactory().getGreeting();        System.out.println(greet.getGreeting());              } } 

As you can see, this application imports the GreetingFactory class and the Greeting interface from the model project. It uses the GreetingFactory to get an instance of the Greeting interface, and then uses the instance of the Greeting interface to print the greeting to the console.

Creating a Project Directory Structure for the Application

The directory structure of the Hello World Java application is as follows :

 Hello World Application root    build.xml +---src        +---xptoolkit            HelloWorld.java \---META-INF         MANIFEST.MF 

Notice the addition of the META-INF directory, which holds the name of the manifest file we will use to make the application's JAR file executable. The only other file that this project needs is not shown; the file is greetmodel.jar, which is created by the model project (the reason for this will become obvious in the following sections).

Creating a Manifest File for a Standalone Application

The goal of this application is for it to work as a standalone JAR file. To do this, we need to modify the manifest file that the application JAR file uses to include the main class and the dependency on greetmodel.jar. The manifest entries that this application needs look something like this:

 Manifest-Version: 1.0 Created-By: Rick Hightower Main-Class: xptoolkit.HelloWorld Class-Path: greetmodel.jar 

The Class-Path manifest entry specifies the JAR files that the JAR file that holds the Hello World Java application needs to run (in our case, greetmodel.jar). The Main-Class manifest entry specifies the main class of the JAR filethat is, the class with the main method that is run when the executable JAR file executes.

Creating an Ant Buildfile for a Standalone Application

The following listing shows the application project buildfile; you'll notice that it is very similar to the model project buildfile. It is divided into the same targets as the model project buildfile: setProps, init, clean, delete, prepare, mkdir, compile, package, and all. The application project buildfile defines the properties differently, but even the property names are almost identical (compare with the model project buildfile earlier in this chapter).

 <project name="application" default="all" >          <target name="setProps" unless="setProps"                            description="setup the properties."> <property name="outdir" value="/tmp/app" />      </target>     <target name="init" depends="setProps"                             description="initialize the properties.">       <tstamp/>       <property name="local_outdir" value="${outdir}/java_app" />       <property name="build" value="${local_outdir}/classes" />       <property name="lib" value="${outdir}/lib" />       <property name="app_jar" value="${lib}/greetapp.jar" />     </target>     <target name="clean" depends="init"                            description="clean up the output directories.">          <delete dir="${build}" />          <delete file="${app_jar}" />     </target>     <target name="prepare" depends="init"                            description="prepare the output directory.">         <mkdir dir="${build}" />         <mkdir dir="${lib}" />     </target>     <target name="compile" depends="prepare"                            description="compile the Java source.">         <javac srcdir="./src" destdir="${build}">             <classpath >                 <fileset dir="${lib}">                     <include name="**/*.jar"/>                 </fileset>             </classpath>         </javac>     </target>     <target name="package" depends="compile"                    description="package the Java classes into a jar.">         <jar jarfile="${app_jar}"              manifest="./META-INF/MANIFEST.MF"            basedir="${build}" />     </target>          <target name="all" depends="clean,package"                            description="perform all targets."/>  </project> 

One of the differences in the application project buildfile is the way that it compiles the Java source:

 <target name="compile" depends="prepare"                            description="compile the Java source.">         <javac srcdir="./src" destdir="${build}">             <classpath >                 <fileset dir="${lib}">                     <include name="**/*.jar"/>                 </fileset>             </classpath>         </javac>     </target> 

Notice that the compile target specifies all the JAR files in the common lib directory (<include name="**/*.jar"/>). The greetmodel.jar file is in the common lib directory, so it is included when the javac task compiles the source. Another difference is the way the application project's buildfile packages the Ant source as follows:

 <target name="package" depends="compile"                    description="package the Java classes into a jar.">         <jar jarfile="${app_jar}"              manifest="./META-INF/MANIFEST.MF"            basedir="${build}" />     </target> 

Notice that the package target uses the jar task as before, but the jar task's manifest is set to the manifest file described earlier. This is unlike the model project buildfile, which did not specify a manifest file; the model used the default manifest file. The application project buildfile's manifest file has the entries that allow us to execute the JAR file from the command line.

In order to run the Hello World Java application, after we run the application project's buildfile, we go to the output common lib directory (tmp/app/lib) and run Java from the command line with the -jar command-line argument, as follows:

 $ java -jar greetapp.jar Hello World! 

You may wonder how it loaded the Greeting interface and GreetingFactory class. This is possible because the manifest entry Class-Path causes the JVM to search for any directory or JAR file that is specified (refer to the JAR file specification included with the Java Platform documentation for more detail). The list of items (directory or JAR files) specified on the Class-Path manifest entry is a relative URI list. Because the greetmodel.jar file is in the same directory (such as /tmp/app/lib) and it is specified on the Class-Path manifest, the JVM finds the classes in greetmodel.jar.

One issue with the application project is its dependence on the model project. The model project must be executed before the application project. How can we manage this? The next section proposes one way to manage the situation with an Ant buildfile.




Professional Java Tools for Extreme Programming
Professional Java Tools for Extreme Programming: Ant, XDoclet, JUnit, Cactus, and Maven (Programmer to Programmer)
ISBN: 0764556177
EAN: 2147483647
Year: 2003
Pages: 228

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net