< Previous | Next >

Lesson 1: Running your Java application outside the workbench

You export your Java application to a directory on your computer that is outside the Java project. Then you configure your Java environment to run your application in the outside directory.
Before you begin
Ensure that you have the following software installed on the computer that runs your Java application:
  • Java developer kit version 1.5 or later
  • IBM® Data Server Driver for JDBC and SQLJ Version 3.57

In the previous module, you created a Java project that contains a Java application in the workbench.

In this lesson, you export your Java application to a directory on your computer, configure the Java environment on your computer, and run the application against the SAMPLE DB2® database.

To run your Java application outside the workbench environment:

  1. Create a test directory that is named C:\pureQuery-test for the Java application and pureQuery files.
  2. In the workbench, update the code in the MySample.java file so that the Java application can run outside the workbench.
    1. Change the default SQL statement that the application runs.

      Set the value of the variable i to 0 in the following code:

          if ( args.length == 0) {
          // value for the default SQL statement to run
             i = 0 ;
    2. Disable the method call that uses pureQuery method DataVersion.

      Add two slashes before myPdqVersion() ; line of code, which calls DataVersion.

          Connection myTestConn=null;
          myTestConn=getConnection();
      
          //print pureQuery information
         // myPdqVersion() ; 
  3. Export the Java application as a JAR file that contains only the Java code without DB2 driver files or pureQuery Runtime files.
    1. In the Package Explorer, right-click the MySample.java file and click Export > Java application.
    2. In the Export window, select Java > JAR file, click Next.
    3. In the JAR file Specification window:
      • In the Select resources to export pane, ensure that only the MySample.java file is selected.
      • Select Export generated class file and resouces.
      • In the JAR file field, specify the output file C:\pureQuery-test\mytestapp.jar.
      • In the Options section, ensure only Compress contents of the JAR file is selected.
    4. Click Finish to create the JAR file.

    The workbench creates the C:\pureQuery-test\mytestapp.jar JAR file. The JAR file contains only the myTestApp\MySample.class file and a MANIFEST.MF file.

  4. Add the JAR files for the JDBC driver the application to the CLASSPATH.
    1. Open a command prompt window, and set the directory to c:\pureQuery-test.

      In the window, enter the command cd c:\pureQuery-test.

    2. Add the files to the CLASSPATH by using the SET command.
      For example, if the DB2 database driver JAR files are installed in the C:\DB2\drivers directory, add the files to the CLASSPATH by using the following command. Enter the SET command on a single line.
      set CLASSPATH=C:\DB2\drivers\db2jcc_license_cu.jar;C:\DB2\drivers\db2jcc.jar;
          C:\pureQuery-test\MyTestApp.jar;%CLASSPATH%;
  5. Run the application:
    Enter the following command in the command prompt window:
    java myTestApp.MySample
    The application runs. Output appears in the command prompt window. The output is similar to the following example:
     =======
    case 0
    case 0 - employee count: 36
    Finished case 0

    The application is not enabled with pureQuery client optimization and does not use any pureQuery Runtime capabilities.

    Note: Do not close the command window. You use the CLASSPATH environment variable set in the command window in the next lesson.

Lesson checkpoint

In this lesson, you exported your Java application ran it outside of the workbench.
You completed the following tasks:
  • Updated your Java application to enable running it outside the workbench.
  • Exported your application as a JAR file.
  • Updated your CLASSPATH to include the DB2 driver JAR files.
  • Ran your application.
< Previous | Next >

Feedback