Collection Contents Index Introduction to debugging Java Using the debugger pdf/chap19.pdf

User's Guide
   PART 3. Java in the Database
     CHAPTER 19. Debugging Java in the Database       

A debugging tutorial


Top of page  Prepare the database

To prepare the sample database for this tutorial, you should run the script jdemo.sql, which installs the Java examples into the database. The class we use in this tutorial is the JDBCExamples class.

For Info     For instructions on how to install the Java examples, see Installing the Java examples.

For Info     For a discussion of the JDBCExamples class and its methods, see Data Access Using JDBC.

Top of page  Prepare to run the Java debugger

Before you can run the debugger, you should ensure that your CLASSPATH environment variable can locate the classes it requires.

  To set your CLASSPATH environment variable:
  1. The Sybase Java debugger is the file Debug.jar, installed in the Java subdirectory of your Adaptive Server Anywhere installation directory. If it is not already present, add this file to your CLASSPATH environment variable.

  2. The debugger uses the Sybase jConnect JDBC driver to connect to the database. Sybase jConnect is the file jdbcdrv.zip in the Java subdirectory of your Adaptive Server Anywhere installation directory. If it is not already present, add this file also to your CLASSPATH environment variable.

Your CLASSPATH environment variable may look as follows after adding these files:

path\Java\Debug.jar;path\Java\jdbcdrv.zip;\jdk1.1.3\lib\classes.zip

where path is your Adaptive Server Anywhere installation directory.

Now you are ready to start the debugger.

Top of page  Start the Java debugger

The debugger runs on your client machine. As it is a Java application itself, it is run using your Java Virtual machine.

You can start the Java debugger from the command line or from Sybase Central.

  To start the Java debugger from Sybase Central:
  1. Start Sybase Central (Windows Edition) and open the Utilities folder, under Adaptive Server Anywhere.

  2. Double-click the Java debugger icon in the right panel.

  To start the Java debugger from a system prompt:
  1. From a system command prompt, change directory to your Adaptive Server Anywhere installation directory.

  2. Enter the following command to run the Java VM (java.exe) using the debugger class:

    java sybase.vm.Debug

    The sybase.vm.Debug class is held in the Debug.jar file.

When you have started the Java debugger, the Connection window appears:

You can now connect to a database from the debugger.

Top of page  Connect to the sample database

To connect to a database, you need to supply a URL, a user ID, and a password. This section describes by example how to connect.

  To connect to the sample database from the debugger:
  1. Start a personal database server on the sample database. You can do this from the Start menu or by entering the following command at a system prompt:

    dbeng6 -c 8M path\asademo.db

    where path is your Adaptive Server Anywhere installation directory.

  2. In the Java debugger, enter the following URL:

    jdbc:sybase:Tds:localhost:2638

    For Info     The meaningsof the components of this URL are described in Supplying a URL for the server.

  3. Enter the user ID DBA and the password SQL and click Connect to connect to the database.

For Info    Once the connection is established, the debugger window displays the message Waiting for a VM. This is because no VM is currently running in the database under the dba user ID. If the dba user ID had one or more current connections carrying out Java operations, one VM for each connection would be listed in the VM list box.

Top of page  Attach to a VM

Here, we start a VM for the dba user ID. You cannot start Java execution from the debugger. To start a VM you must carry out a Java operation from another connection under the same user ID.

  To attach to a VM:
  1. With the debugger running, connect to the sample database from Interactive SQL as user ID DBA.

  2. Execute some Java code using the following statement:

    SELECT JDBCExamples.Query()

    The Sybase Java VM starts in order to retrieve the Java objects from the table. The debugger immediately stops execution of the Java code. You will see that the command in ISQL does not complete (the Execute button is grayed out, and no result set is displayed).

    The debugger Connection window lists the VM in its list of available VMs.

  3. In the debugger Connection window, click the VM and click Attach to VM. The debugger attaches to the VM and the Source window appears. At this point the Connection window disappears.

    The Source window is empty. The next step is to enable the Source window to show the source code for the method. The source code is available on disk.

Top of page  Load source code into the debugger

The debugger looks in a set of locations for source code files (with .java extension). You need to add the jxmp subdirectory of your installation directory to the list of locations, so that the code for the class currently being executed in the database is available to the debugger.

  To add a source code location to the debugger:
  1. From the Source window, select File->Source Path. The Source Path window displays.

  2. From the Source Path window, select Path->Add. Enter the following location into the text box:

    path\jxmp

    where path is the name of your Adaptive Server Anywhere installation directory.

    The source code for the JDBCExamples class displays in the window, with the first line of the Query method highlighted. The Java debugger has stopped execution of the code at this point.

    You can now close the Source Path window.

Top of page  Step through source code

You can step through source code in the Java debugger in several ways. In this section we illustrate the different ways you can step through code using the Query method.

When execution pauses at a line until you provide further instructions, we say that the execution breaks at the line. The line is a breakpoint. Stepping through code is a matter of setting explicit or implicit breakpoints in the code, and executing code to that breakpoint.

Following the previous section, the debugger should have stopped execution of JDBCExamples.Query at the first statement:

Examples 

Here are some example steps you can try:

  1. Step to the next line     Press F7 to step to the next line in the current method. Try this two or three times.

  2. Run to a selected line     Select the following line (line 284) using the mouse, and press F6 to run to that line and break:

    max_price = price;
  3. Set a breakpoint and execute to it     Select the following line (line 292) and press F9 to set a breakpoint on that line:

    return max_price;

    An asterisk appears in the left hand column to mark the breakpoint. Press F5 to execute to that breakpoint.

  4. Experiment     Try different methods of stepping through the code. End with F5 to complete the execution.

    When you have completed the execution, the Interactive SQL Data window displays the value 24.

Options 

The complete set of options for stepping through source code are displayed on the Run menu. They include the following:

Function

Shortcut key

Description

Run

F5

Continue running until the next breakpoint, until the Stop item is selected, or until execution finishes.

Step Over

F7 or Space

Step to the next line in the current method. If the line steps into a different method, step over the method, not into it. Also, step over any breakpoints within methods that are stepped over.

Step Into

F8 or i

Step to the next line of code. If the line steps into a different method, step into the method.

Step Out

F11

Complete the current method, and break at the next line of the calling method.

Stop

Break execution.

Run to Selected

F6

Run until the currently selected line is executed and then break.

Home

F4

Select the line where the execution is broken.

Top of page  Inspecting and modifying variables

You can inspect the values of both local variables (declared in a method) and class static variables in the debugger.

Inspecting local variables 

You can inspect the values of local variables in a method as you step through the code, to better understand what is happening. You must have compiled the class with the javac -g option to do this.

  To inspect and change the value of a variable:
  1. Set a breakpoint at the first line of the JDBCExamples.Query method. This line is as follows:

    int max_price = 0
  2. In Interactive SQL, enter the following statement again to execute the method:

    SELECT JDBCExamples.Query()

    The query executes only as far as the breakpoint.

  3. Press F7 to step to the next line. The max_price variable has now been declared and initialized to zero.

  4. From the Source window, select Window->Locals. The Local window appears.

    The Locals window shows that there are several local variables. The max_price variable has a value of zero. All others are listed as not in scope, which means they are not yet initialized.

  5. In the Source window, press F7 repeatedly to step through the code. As you do so, the values of the variables appear in the Locals window.

    If a local variable is not a simple integer or other quantity, then as soon as it is set a + sign appears next to it. This means the local variable has fields that have values. You can expand a local variable by double-clicking the + sign or setting the cursor on the line and pressing Enter.

  6. Complete the execution of the query to finish this exercise.

Modifying local variables 

You can also modify values of variables from the Locals window.

  To modify a local variable:
  1. In the debugger Source window, set a breakpoint at the following line in the Query method of the JDBCExamples class:

    int max_price = 0
  2. Step past this line in the execution.

  3. Open the Locals window. Select the max_price variable, and select Local->Modify. Alternatively, you can set the cursor on the line and press Enter.

  4. Enter a value of 45 in the text box, and click OK to confirm the new value. The max_price variable is set to 45 in the Locals window.

  5. From the Source window, press F5 to complete execution of the query. In the Interactive SQL Data window, the value 45 is returned from the function.

Inspecting static variables 

You can also inspect the values of class-level variables (static variables).

  To inspect a static variable:
  1. From the debugger Source window, select Window->Classes. The Classes window is displayed.

  2. Select a class in the left hand box. The methods and static variables of the class are displayed in the right hand boxes.

  3. Select Static->Inspect. The Inspect window is displayed. It lists the variables available for inspection.

Top of page  

Collection Contents Index Introduction to debugging Java Using the debugger pdf/chap19.pdf