Debug operations

You can use the interactive display of your server to use the *DEBUG option to view the source code before running the program. Then, you can set breakpoints, or step over or into a program to analyze errors while the program is running.

Debugging Java programs using the *DEBUG option

To debug Java™ programs using the *DEBUG option, follow these steps:
  1. Compile the Java program by using the DEBUG option, which is the -g option on the javac tool.
  2. Insert the class file (.class) and source file (.java) in the same directory on your server.
  3. Run the Java program by using the Run Java (RUNJVA) command on the IBM® i command line. Specify OPTION(*DEBUG) on the Run Java (RUNJVA) command. For example: RUNJVA CLASS(classname) OPTION(*DEBUG)

    Only a class may be debugged. If a JAR file name is entered for the CLASS keyword, OPTION(*DEBUG) is not supported.

  4. The Java program source is displayed.
  5. Press F6 (Add/Clear breakpoint) to set breakpoints, or press F10 (Step) to step through the program.
Note:
  • While using breakpoints and steps, check the logical flow of the Java program, then view and change variables, as necessary.
  • Using OPTION(*DEBUG) on the RUNJVA command disables the Just-In-Time (JIT) compiler.
  • If you are not authorized to use the Start Service Job (STRSRVJOB) command, OPTION(*DEBUG) is ignored.

Debugging Java programs from another display

When debugging a Java program by using the interactive display of your server, the program source displays whenever it encounters a breakpoint. This may interfere with the display output of the Java program. To avoid this, debug the Java program from another display. The output from the Java program displays where the Java command is running and the program source shows on the other display.

It is also possible to debug an already running Java program in this manner as long as it has been started with debug enabled.
Note: You can enable Java debug by adding the AGTPGM(D9TI) option to the JAVA/RUNJVA command in order to use the IBM i Debugger with the JVM. AGTPGM(D9TI) is not needed when using OPTION(*DEBUG).
To debug Java from another display, do the following:
  1. The Java program must be held while you start setting up to debug.
    You can hold the Java program by making the program:
    • Wait for input from the keyboard.
    • Wait for a time interval.
    • Loop to test a variable, which requires that you set a value to eventually get the Java program out of the loop.
  2. Once the Java program is held, go to another display to perform these steps:
    1. Enter the Work with Active Jobs (WRKACTJOB) command on the command line.
    2. Find the batch immediate (BCI) job where your Java program is running. Look under the Subsystem/Job listing for QJVACMDSRV. Look under the User listing for your User ID. Look under Type for BCI.
    3. Enter Option 5 to work with that job.
    4. At the top of the Work with Job display, the Number, User, and Job are displayed. Enter STRSRVJOB Number/User/Job
    5. Enter STRDBG CLASS(classname). Classname is the name of the Java class that you want to debug. It can either be the class name that you specified on the Java command, or it can be another class.
    6. The source for that class appears in the Display Module Source display.
    7. Set breakpoints, by pressing F6 (Add/Clear breakpoint), whenever you would like to stop in that Java class. Press F14 to add other classes, programs, or service programs to debug.
    8. Press F12 (Resume) to continue running the program.
  3. Stop holding your original Java program. When the breakpoints are hit, the Display Module Source display appears on the display where the Start Service Job (STRSRVJOB) command and the Start Debug (STRDBG) command were entered. When the Java program ends, a Job being serviced ended message appears.
  4. Enter the End Debug (ENDDBG) command.
  5. Enter the End Service Job (ENDSRVJOB) command.

When you debug a Java program, your Java program is actually running in the Java virtual machine in a batch immediate (BCI) job. Your source code displays in the interactive display, but the Java program is not running there. It is running in the other job, which is a serviced job. See the QIBM_CHILD_JOB_SNDINQMSG environment variable topic for more information about this variable that controls whether the BCI job waits before calling the Java virtual machine.