Compiling and debugging Java programs

This chapter presents overviews of compiling and debugging. It contains the following sections: See also:
Building Applications with JBuilder: Compiling Java programs
Building Applications with JBuilder: Debugging Java programs


Compiling Java programs

The JBuilder compiler has full support for the Java language, including inner classes and JAR files. You can compile from within the IDE, or from the command line using bmj (Borland Maker for Java) or bcj (Borland Compiler for Java).

A Java compiler reads Java source files, determines which additional files need to be compiled, and produces the Java program in the form of .class files containing bytecodes that are the machine code for the Java Virtual Machine (VM). Compiling produces a separate .class file for each class declaration and interface declaration in a source file. When you run the resulting Java program on a particular platform, such as Windows NT, the Java interpreter for that platform runs the bytecodes contained in the .class files.

The following parts of a program can be compiled:

Types of compiling

The Build menu contains the two commands for compiling: Make and Rebuild:

The node can be a project, a package, or a .java file. Packages lower in the project tree hierarchy are built before packages higher in the hierarchy.

Compiling a program

To compile the source files for an application or applet:
  1. Open the project containing the program, or open a single Java file.

  2. In the Navigation pane, do one of the following:

  3. Then, do one of the following:

Any error messages are displayed in the Error pane that is added to the AppBrowser below the Content pane. Select an error message to display the associated source code. To get help on the error message, go to the Error Messages topic, where error messages are listed by number.

To select another error message in the Error pane, use View|Next Error Message or View|Previous Error Message.

Setting compiler options

You can specify compiler options for the current project. The options are applied to all files in the project tree. The options are also applied to files referenced by these files, stopping at packages that are marked "stable" and have no classes in the project tree.

To set compiler options:

  1. Select the project node in the Navigation pane.

  2. Right-click and choose Properties.
    The Project Properties dialog box is displayed.

  3. Click the Compiler tab to display the Compiler page.

  4. Choose the following options:

    Option Description

    Include Debug Information Includes symbolic debug information in the .class file when you compile, make, or rebuild a node.
    Show Warnings Displays compiler warning messages.
    Show Deprecations Displays all deprecated classes, methods, properties, events, and variables used in the API. If a warning is displayed when compiling, indicating that some deprecated APIs were used, you can turn this option on to see all deprecated APIs.
    Check Stable Packages Checks files in the packages that are marked "stable" to see whether they and their imported classes need to be recompiled. This option shortens the edit/recompile cycle by not re-checking stable packages.

    If this option is off, a given branch of the checking process halts when it reaches a package marked "stable;" it does not look for unstable packages imported by the stable packages. Therefore, with this option, you might need to specify a greater number of modules to compile.

    By default, the compiler checks packages marked "stable" as well as packages marked "unstable," to determine whether they and their imported classes need to be recompiled.

    Make Packages Stable If this option is on, the compiler will compile or check all the classes of a package on the first build and mark the package "stable." If this option is off, only the referenced classes of this package will be made, and the package will not be marked "stable."

    This option should be off when working with partial projects.

    This option is especially useful for working with a library of classes with no source available, when some of the class files are not consistent, but not used.

    Obfuscate Obfuscates your code.

    Obfuscation makes your programs less vulnerable to reverse engineering. After decompiling your obfuscated code, the generated source code contains altered symbol names for private symbols.

    Exclude Class Excludes the selected .class file from a compile.
    Encoding Specifies the encoding that controls how the compiler interprets characters beyond the ASCII character set. If no setting is specified, the default native-encoding converter for the platform is used.
    Autosave All Files Automatically saves all files in the project.

  5. Click OK to close the Project Properties dialog box and save your settings.
Note:
The options set on the Compiler page of the Default Project Properties dialog (Tools|Default Project Properties) are used for new projects.


Debugging Java programs

Modifying program data values during a debugging session provides a way to test hypothetical bug fixes during a program run. If you find that a modification fixes a program error, you can exit the debugging session, fix your program code accordingly, and recompile the program to make the fix permanent.

When the Debugger pauses your program, you can look at the current values of the program data items, including class, instance, and local variables; method parameters; and properties (getter/setter method pairs). You can examine and change the values of data items with the following:

Pane, Window, or Dialog Purpose

Threads and Stack pane Displays the running thread groups and threads. The selected thread is the current context. Also displays the sequence of method calls that brought the program to its current state. This is the top pane of the Debug tab, by default.
Data pane Displays the values of all data members that are currently in use. This is the bottom pane of the Debug tabbed-page, by default.
Watch pane Displays the values of specific data members that you have selected for tracking. This is the Watch tabbed-page.
Inspector window Displays the current value of a variable, evaluated in the context used when the Inspector was created.
Evaluate/Modify dialog box Evaluates or changes the value of an expression or property.

Many debugger features are accessed through the Run and View menus.

The following is an overview of the complete process for debugging Java code. Your code must be compiled with debugging information before you can use the Debugger.

Compiling your project and starting the Debugger

To compile your project and start the Debugger,
  1. Open your project.
    The project appears in the AppBrowser.
  2. Select File|Project Properties. On the Compiler page, check the Include Debug Information option.
  3. In the Navigation pane, select the .jpr project node. Choose Run|Debug, click the Debug icon on the toolbar, or press Shift + F9.
    The node automatically compiles, using Build|Make. If the project or class compiles successfully, the Debugger appears.
The left side of the Debugger window now has additional tabs: See also:
The Debugger window

Setting breakpoints and stepping through your code

To set breakpoints,
  1. In the Source pane, set a breakpoint on an executable statement (a statement with a blue dot in the margin) by clicking in the margin to the left of the statement.
    A red circle with a check mark appears in the left margin, indicating that the breakpoint is valid.
  2. Choose Run|Debug or click the Debug icon in the toolbar.
    The class runs and stops at the breakpoint.
  3. To move through your code, select Run|Trace Into to trace into a method call or Run|Step Over to step over a method call.
  4. Look in the Threads and Stack pane to examine all the program's threads and the stack for each method called.
  5. Double-click on a method in the stack to display the associated source code.

Editing and recompiling

To edit and recompile your source code,
  1. When you have found lines of code to change, end the debugging session by clicking the red stop sign icon in the upper left of the Navigation pane, or by choosing Run|Program Reset.
  2. Edit your code in the Source pane.
  3. Right-click the .java node, and select Build|Make on the main menu. The affected files in your project are recompiled, and you can run the Debugger again.