Step 6: Compiling and Running the sample application

Compiling the sample application

Now that you've generated your object model with Torque, and created a sample application, you are now ready to compile everything. Again, Maven is used to control the build process.

First, we need to tell maven where the java source files are located. To do this, add the following section just after the <dependencies> of your project.xml which you created sooner on in the top level directory of your project:

  <build>
    <sourceDirectory>src/java</sourceDirectory>
  </build>

Afterwards, you can compile your jave source files by typing the following in the top-level directory of your project:

maven java:compile

If you've done everything correctly, this should build without any errors. All of the resulting Java class files are placed in the target/classes directory. If the compiler misses any external libraries, review the <dependencies> section in your project.xml. Should you encounter other errors, go back and review your application code.

Running the sample application

Before you run the sample application, you must first set your classpath (this was done automatically for you by Maven when you compiled). The classpath must include: all of the jars denoted in the <dependencies> section of your project.xml, and all of your application and object model classes located in target/classes.

Before compiling your application, maven will have downloaded all libraries your project depends on to the local maven repository. Gathering them from there is a cumbersome process. To ease copying them into one place, we modify Maven's build process a little. For this, you need to create a new file called maven.xml in the top-level directory of your project. It needs to have the following content:

<project xmlns:java="java" xmlns:deploy="deploy">
  <postGoal name="java:compile">
    <!-- include listed dependencies -->
    <deploy:copy-deps todir="${maven.build.dir}/lib"/>
    <!-- include torque.properties -->
    <copy file="src/conf/torque.properties" todir="${maven.build.dir}" />
  </postGoal>
</project>

After creating the file, re-run

maven java:compile

Now change into the target directory. In addition to the classes subdirectory which was already created in the first compilation run, you see that maven has generated an additional lib directory which contains the needed libraries, and that maven also copied the Torque.properties file here.

Now everything is in one place, and you can start the application by typing (note:exchange the mysql-connector-java-5.0.4.jar by the datatbase driver jar you are using)

java -cp classes:lib/avalon-framework-api-4.3.jar:lib/commons-beanutils-core-1.7.0.jar:lib/commons-collections-3.2.jar:lib/commons-configuration-1.4.jar:lib/commons-dbcp-1.2.2.jar:lib/commons-lang-2.3.jar:lib/commons-logging-1.1.jar:lib/commons-pool-1.3.jar:lib/jcs-1.3.jar:lib/mysql-connector-java-5.0.4.jar:lib/torque-3.3.jar:lib/village-3.3.jar com.kazmier.Bookstore

If all goes well, you should see the following output:

  Full booklist:

  Title:     TCP/IP Illustrated, Volume 1
  ISBN:      0-201-63346-9
  Publisher: Addison Wesley Professional
  Author:    Stevens, W.

  Title:     Effective Java
  ISBN:      0-618-12902-2
  Publisher: Addison Wesley Professional
  Author:    Bloch, Joshua

  Booklist (specific ISBN):

  Title:     TCP/IP Illustrated, Volume 1
  ISBN:      0-201-63346-9
  Publisher: Addison Wesley Professional
  Author:    Stevens, W.

  Booklist (authors swapped):

  Title:     TCP/IP Illustrated, Volume 1
  ISBN:      0-201-63346-9
  Publisher: Addison Wesley Professional
  Author:    Bloch, Joshua

  Title:     Effective Java
  ISBN:      0-618-12902-2
  Publisher: Addison Wesley Professional
  Author:    Stevens, W.

  Booklist (should be empty):

If your application throws an exception, it could be for one of many reasons, most of which are not very descriptive unfortunately. Do not be discouraged if your application does not run the first time. Carefully retrace all of the steps outlined in this tutorial. If you are still not able to get your application to run, use the Torque user mailing list to your advantage.

Where to Go From Here

Congratulations! You have completed the Torque tutorial. Although this has only been an introduction to Torque, it should be sufficient to get you started with Torque in your applications. For those of you seeking additional information, there are several other documents on this site that can provide details on various subjects. Lastly, the source code is an invaluable resource when all else fails to provide answers!

User Comments

User comments for this step