September 15, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Struts from Scratch

  • March 10, 2003
  • By Kevin Bedell
  • Send Email »
  • More Articles »

Compiling and Running the Hello World Example Application

One of the critical factors in being successful as a developer is making sure you have an efficient Edit/Compile/Test process. The goal of this section is to provide a simple and efficient way to get this process organized.

There are many options with regard to the 'editing' part; they range from text editors such as Notepad, vi, and emacs up through powerful IDEs such as JBuilder, IntelliJ, and Eclipse. (Personally, I use Eclipse from http://www.eclipse.org). This article doesn't cover editing options, so let's discuss the build and test parts of the equation in a bit more detail.

The value that Ant provides is that it greatly simplifies the build and test process. Ant uses an XML script to direct all the actions required for compiling the Java files, building the WAR file for you, and then deploying the WAR file into your servlet container. In the case of Tomcat, deploying the WAR file means just copying it into the TOMCAT_HOME\webapps directory (where TOMCAT_HOME represents the top-level directory for your Tomcat installation, for example C:\dev\tomcat).

Getting the Example Application

The example application is available at http://www.strutskickstart.com. It is the Hello World! application. To install it, simply download it and copy it into a convenient directory on your computer.

For this sample application, an Ant script is already written. This Ant script loads the properties it needs to work on a particular computer from a properties file. This properties file contains the directory paths it needs to locate the Tomcat installation on your machine. This file is named build.properties and is shown below:

// The build.properties file.

    tomcat.home=c:/dev/tomcat
    webapps.home=c:/dev/tomcat/webapps

To modify this file, simply edit it and replace these two values with the appropriate values for your computer.

The main Ant build script is named build.xml. (This is the default Ant build file name.) After you modify the build.properties file, the build script should run with no other modifications. We'll go through running the build script in just a moment.

There is one section of the build file that may need to be modified to work with your version of Tomcat (or whatever servlet container you use). Specifically, the "Compilation Classpath" section may need to be modified to ensure that it contains the correct directory paths. If the build script expects directories to be there that aren't, then the build will fail.

Here is what that section looks like:

// The compilation classpath section from the build.xml
// ant script.

<path id="compile.classpath">

    <!-- The object files for this application -->
    <pathelement location="${object.home}"/>

    <!-- The lib files for this application -->
    <fileset dir="${lib.home}">
      <include name="*.jar"/>
      <include name="*.zip"/>
    </fileset>

    <!-- All files/jars that Tomcat makes available -->
    <fileset dir="${tomcat.home}/common/lib">
      <include name="*.jar"/>
    </fileset>
    <pathelement location="${tomcat.home}/common/classes"/>

  </path>

As you can see from the listing above, there are three pieces to the compilation classpath. The first two—the location of all your application object files (the compiled Java files in your application) and the library files for the sample application (the '.jar' files that contain the external libraries that the sample application uses)—come with the sample application and don't need to be changed. The third section—the files/jars that Tomcat makes available—may need to be changed.

This section would need to be changed if either 1) you are using a version of Tomcat that stores its 'lib' and 'classes' directories in a different location, or 2) you are using a servlet container other than Tomcat. If you look in the TOMCAT_HOME\common\lib directory and see files named servlet.jar and tools.jar, you probably won't need to change anything. If you don't see these files there, try to locate them and modify the build.xml file so that the compilation classpath contains the right paths to these files.

After you have modified the build.properties file and validated the classpath section of the build.xml file, you are ready to build and deploy the application.

Running the Build Script and Testing the Application

Now, everything should be all set to build and test the application using the Ant build script provided.

Ant build scripts usually allow you to do more than just build and deploy the application; this build script is no exception. When a build script allows you to accomplish more than just one action, it does so by providing what are called different 'targets.' Each target in a build script represents a collection of different tasks that need to be accomplished. For example, a build script might contain a target to build and deploy an application, another target to delete all the compiled code so you can start from scratch, and potentially other targets to run unit tests or check out the latest versions of all the files from a source control system.

The build script provided for the sample application provides a number of targets. To display the list of targets that it can build, change to the directory containing the build.xml file and simply type ant. The output you should see is provided below:

// Typing only 'ant' without specifying a target to build.

C:\dev\examples\ch03\hello>ant
Buildfile: build.xml

help:
     [echo] Please specify a target! [usage: ant ]
     [echo] Here is a list of possible targets:
     [echo]   clean-all.....Delete build dir, all .class and
                            war files
     [echo]   prepare.......Creates directories if required
     [echo]   compile.......Compiles source files
     [echo]   build.........Build war file from .class and
                            other files
     [echo]   deploy........Copy war file to the webapps directory
     [echo]   javadoc.......Generates javadoc for this application

BUILD SUCCESSFUL
Total time: 5 seconds
C:\dev\examples\ch03\hello>

What you are seeing isn't default Ant behavior. There is actually another 'target' defined in this file that simply prints the message you see. By creating a target to do this and then setting it to be the default target (the target executed if no other target is specified), you can create a simple way to tell users what targets the build file knows how to execute.

Looking at the output from this command, you can see that the target we need to execute to build and deploy our sample application is ant deploy. This command compiles all the source files and creates the final hello.war file that contains the application. Once this file is created, it is copied into your Tomcat webapps directory where it can be loaded and run.

Below is the output you should see when you type the command ant deploy:

// Typing 'ant deploy' to build the WAR file and deploy it into
// your Tomcat webapps directory.

C:\dev\examples\ch03\hello>ant deploy
Buildfile: build.xml

prepare:
     [echo] Tomcat Home  = c:/dev/tomcat
     [echo] webapps Home = c:\dev\tomcat\webapps
    [mkdir] Created dir: C:\dev\examples\ch03\hello\object
    [mkdir] Created dir: C:\dev\examples\ch03\hello\deploy
    [mkdir] Created dir: C:\dev\examples\ch03\hello\doc
    [mkdir] Created dir: C:\dev\examples\ch03\hello\doc\api
    [mkdir] Created dir: C:\dev\examples\ch03\hello\build
    [mkdir] Created dir: C:\dev\examples\ch03\hello\build\WEB-INF
    [mkdir] Created dir: C:\dev\examples\ch03\hello\build\
                            WEB-INF\classes
    [mkdir] Created dir: C:\dev\examples\ch03\hello\build\
                            WEB-INF\lib

compile:
    [javac] Compiling 4 source files to C:\dev\examples\ch03\
                                           hello\object

build:
     [copy] Copying 11 files to C:\dev\examples\ch03\hello\build
     [copy] Copying 4 files to C:\dev\examples\ch03\hello\build\
                                  WEB-INF\classes
     [copy] Copying 1 file to C:\dev\examples\ch03\hello\build\
                                 WEB-INF\classes
     [copy] Copying 9 files to C:\dev\examples\ch03\hello\build\
                                  WEB-INF\lib
      [jar] Building jar: C:\dev\examples\ch03\hello\deploy\
                             hello.war

deploy:
     [copy] Copying 1 file to C:\dev\tomcat\webapps

BUILD SUCCESSFUL
Total time: 27 seconds

Once this step has completed, you should be able to point a browser to http://localhost:8080/hello and see the application (assuming you are using Tomcat and it is installed locally on the default port of 8080). If you have problems seeing the application, you may need to restart your Tomcat instance.

The sample application is pretty simple—it just allows you to type the name of someone to say 'Hello!' to, then it echoes their name. A detailed description of this application and all its components is provided in my book, Struts Kick Start.

Once you understand the basics of how to install, build, and deploy this simple Struts application, you will be ready to move up and begin developing more complex applications on your own. Happy Coding!

About the Author

Kevin Bedell has a degree in Engineering from Michigan Tech and an MBA from the Crummer Graduate School of Business at Rollins College. Portions of this article were adapted from material in his upcoming book for SAMS Publishing, Struts Kick Start (ISBN: 0-672-32472-5). A companion PowerPoint presentation suitable for presenting these ideas to a management group is available at http://www.strutskickstart.com.



Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel