Java Programming After this Course
In this lesson, we will look at what you will need and the steps you will take to write, execute, and debug a Java program.
We'll cover the following
Creating and running your program
You will need a text editor and additional tools to write and run Java application programs outside of this course. If your platform is Windows or UNIX, you can download the free Java SE Development Kit (JDK) as described in the next appendix. If you are a macOS user, Java is included with your operating system.
You might prefer to use an integrated development environment (IDE), which provides an editor, compiler, Java Virtual Machine, and other helpful tools for writing Java programs. The next appendix also provides links to these resources.
Writing the program
You can use any simple, plain-text editor or one supplied with an IDE to write a Java program. An IDE’s editor typically uses colors to highlight various components of the program. As you type your program, the characters are stored in the computer’s primary memory.
You risk losing your work if you do not save it in a file on your computer. Get in the habit of saving your work often; do not wait until you have finished typing the entire program. Note that Java expects the name of the file to be name.java
, where name is the name of the program.
Compiling the program
Once you have your program saved in a file, you will compile it. With an IDE, clicking a button will accomplish this step. With the JDK, however, you would type the command:
javac name.java
After compilation, the compiler saves the bytecode in a file named name.class
.
Executing the program
You now can run the program. In an IDE, clicking a button will execute the bytecode. Frequently, one button both compiles and executes a program, but if compilation is unsuccessful, execution does not occur. Using the JDK, you run a program by typing the command:
java name
Notice that this command uses the name of the program, not the file named name.class
.
Debugging your program
The lesson Assertions introduced assertions as a way to check a program’s logic during its development. Overview of Debugging Tools mentioned various debugging tools, such as breakpoints and single-stepping that a typical IDE provides. Here you will see how to accomplish these techniques using command-line tools in the Java Development Kit (JDK).
Enabling assertions
When you execute a Java program using the command:
java name
Any assert
statements in the program are ignored by default. To enable them during execution, you must add -enableassertions
to the previous command, as follows:
java -enableassertions name
The next time that you execute the program, assertions will be ignored, unless you explicitly enable them again.
Enabling command-line debugging tools
Before you can use the command-line debugging tools, you first must tell the compiler to generate some debugging information. You do this by providing the -g
option to the javac
command when you compile your program, as follows:
javac -g class_name.java
Then, instead of using the command java
to run the program, you use jdb
.
For example, to compile
and debug the program MyProgram
, you would use the following sequence of commands:
javac -g MyProgram.java jdb MyProgram
The response will be
Initializing jdb ... >
At the prompt >
, you can tell the debugger to set breakpoints, trace execution, or exit the debugger. We describe each of these requests in the next sections.
Setting and using breakpoints
Setting breakpoints
To request a breakpoint in MyProgram
at line 15, you type
stop at MyProgram:15
After the prompt >
. The system response is
Deferring breakpoint MyProgram:1 It will be set after the class is loaded. >
Stepping through execution
After requesting as many breakpoints as you like, you begin program execution by typing:
run
After the >
prompt. When the breakpoint at line 15 is reached, you will receive a message and the statement at that line will be displayed.
This statement will execute next when you use the command:
step
Each time you type step
, the statement at the current line executes and the debugger advances to the next line.
Looking at the contents of variables
You can look at the current value of any variable by using the print
command. For example,
to see the current value of index
, you would type:
print index
after the prompt >
.
Continuing execution
You can continue single-stepping with the step
command or you can resume execution until the next breakpoint by using the command
cont
Removing a breakpoint
To remove a breakpoint, you use the clear
command. For example, to clear the breakpoint we
set previously at line 15, you would type
clear MyProgram:15
Tracing methods
After setting a breakpoint and beginning execution, you can trace when methods are entered and exited by giving the command:
trace methods
Now each time you type:
cont
execution continues and then stops either at the beginning of a method or at its end. Tracing methods in this manner is just like setting breakpoints at the beginning and end of each method.
Ending debugging
To end a debugging session, you type the command:
exit
📝 Note:
jdb
commandsThe debugger
jdb
offers more commands than the ones we have introduced here. To see a list of these commands while you are within the debugger, you type the commandhelpIf you have not begun a debugging session, you would first type the command
jdb
, wait for the system response, and then typehelp
. The result would appear, as follows:jdb Initializing jdb ... help
📝 Note: Debugging in an IDE
Debugging actions like those we have presented here are available in a typical IDE, although the particulars of how to take those actions differ. If you are using an IDE, you should learn its debugging tools, as they will be more convenient and easier to use than the command-line tools. The approach to debugging, however, is the same regardless of the form the tools take.
Get hands-on with 1400+ tech skills courses.