Sybase Technical Library - Product Manuals Home
[Search Forms] [Previous Section with Hits] [Next Section with Hits] [Clear Search] Expand Search

Chapter 5 XML in

the Database [Table of Contents] Chapter 7 Reference Topics

Java in Adaptive Server Enterprise

[-] Chapter 6 Debugging Java in the Database

Chapter 6

Debugging Java in the Database

This chapter describes the Sybase Java debugger and how you can use it when developing Java in Adaptive Server.

Introduction to Debugging Java

You can use the Sybase Java debugger to test Java classes and fix problems with them.

How the Debugger Works

The Sybase Java debugger is a Java application that runs on a client machine. It connects to the database using the Sybase jConnect JDBC driver.

The debugger debugs classes running in the database. You can step through the source code for the files as long as you have the Java source code on the disk of your client machine. (Remember, the compiled classes are installed in the database, but the source code is not).

Requirements for Using the Java Debugger

To use the Java debugger, you need:

What You Can Do with the Debugger

Using the Sybase Java debugger, you can:

Using the Debugger

This section describes how to use the Java debugger. The next section provides a simple tutorial.

Starting the Debugger and Connecting to the Database

The debugger is the JAR file Debug.jar, installed in your Adaptive Server installation directory in $SYBASE/$SYBASE_ASE/debugger. If it is not already present, add this file as the first element to your CLASSPATH environment variable.

Debug.jar contains many classes. To start the debugger you invoke the sybase.vm.Debug class, which has a main( ) method.You can start the debugger in three ways:

Compiling Classes for Debugging

Java compilers such as the Sun Microsystems javac compiler can compile Java classes at different levels of optimization. You can opt to compile Java code so that information used by debuggers is retained in the compiled class files.

If you compile your source code without using switches for debugging, you can still step through code and use breakpoints. However, you cannot inspect the values of local variables.

To compile classes for debugging using the javac compiler, use the -g option:

javac  -g ClassName.java

Attaching to a Java VM

When you connect to a database from the debugger, the Connection window shows all currently active Java VMs under the user login name. If there are none, the debugger goes into wait mode. Wait mode works like this:

The Source Window

The Source window:

The Debugger Windows

The debugger has the these windows:

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.

Setting Breakpoints

When you set a breakpoint in the debugger, the Java VM stops execution at that breakpoint. Once execution is stopped, you can inspect and modify the values of variables and other expressions to better understand the state of the program. You can then trace through execution step by step to identify problems.

Setting breakpoints in the proper places is a key to efficiently pinpointing the problem execution steps.

The Java debugger allows you to set breakpoints not only on a line of code, but on many other conditions. This section describes how to set breakpoints using different conditions.

Breaking on a Line Number

When you break on a particular line of code, execution stops whenever that line of code is executed.

To set a breakpoint on a particular line:

When a breakpoint is set on a line number, the breakpoint is shown in the Source window by an asterisk in the left column. If the Breakpoints window is open, the method and line number is displayed in the list of breakpoints.

You can toggle the breakpoint on and off by repeatedly double-clicking or pressing F9.

Breaking on a Class Method

When you break on a method, the break point is set on the first line of code in the method that contains an executable statement.

To set a breakpoint on a class method:

When a breakpoint is set on a method, the breakpoint is shown in the Source window by an asterisk in the left column of the line where the breakpoint actually occurs. If the Breakpoints window is open, the method is displayed in the list of breakpoints.

Using Counts with Breakpoints

If you set a breakpoint on a line that is in a loop, or in a method that is frequently invoked, you may find that the line is executed many times before the condition you are really interested in takes place. The debugger allows you to associate a count with a breakpoint, so that execution stops only when the line is executed a set number of times.

To associate a count with a breakpoint:

Using Conditions with Breakpoints

The debugger allows you to associate a condition with a breakpoint, so that execution stops only when the line is executed and the condition is met.

To associate a condition with a breakpoint:

Breaking When Execution Is Not Interrupted

With a single exception, breakpoints can only be set when program execution is interrupted. If you clear all breakpoints, and run the program you are debugging to completion, you can no longer set a breakpoint on a line or at the start of a method. Also, if a program is running in a loop, execution is continuing and is not interrupted.

To debug your program under either of these conditions, select Run-->Stop from the Source window. This stops execution at the next line of Java code that is executed. You can then set breakpoints at other points in the code.

Disconnecting from the Database

When the program has run to completion, or at anytime during debugging, you can disconnect from the database from the Connect window. Then, exit the Source window and reconnect to the database after the debug program terminates.

A Debugging Tutorial

This section takes you through a simple debugging session.

Before You Begin

The source code for the class used in this tutorial is located in $SYBASE/$SYBASE_ASE/sample/JavaSql/manual-examples/JDBCExamples.java.

Before you run the debugger, compile the source code using the javac command with the -g option.

See "Creating Java Classes and JARs" on page 22 for complete instructions for compiling and installing Java classes in the database.

Start the Java Debugger and Connect to the Database

You can start the debugger and connect to the database using a script, command line options, or Sybase Central. In this tutorial, we use jdebug to start the debugger. You can use any database.

Follow these steps:

Once the connection is established, the debugger window displays a list of available Java VMs or "Waiting for a VM."

Attach to a Java VM

To attach to a Java VM from your user session:

Load Source Code into the Debugger

The debugger looks for source code files. You need to make the $SYBASE/$SYBASE_ASE/sample/JavaSql/manual-examples/ subdirectory available to the debugger, so that the debugger can find source code for the class currently executing in the database.

To add a source code location to the debugger:

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 serverMain( ) 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.serverMain( ) at the first statement:

Examples

Here are some steps you can try:

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.

To inspect and change the value of a variable:

Modifying Local Variables

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

To modify a local variable:

Inspecting Static Variables

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

To inspect a static variable:


XML ResultSet Documents: Invalid XML Characters [Table of Contents] Chapter 7 Reference Topics