Using Assertions in Java

This document's scope is limited to how to go about compiling and executing Java classes/applications in order to make assert commands operative. (By default, such commands are ignored.) For a broader discussion about the use of the assert command, go to the tutorial on Sun's web site.

From jGrasp

  1. On the Menu Bar, click on Settings.
  2. Move down to the Compiler Settings item on the menu.
  3. Choose Workspace. A window titled "Settings for workspace" should appear.
  4. On the "Settings for workspace" window, click on the Flags / Args / Main button.
  5. On the left side (under FLAGS or ARGS), in the text box next in the same row as the word Compile, enter the string -source 1.5. (You first have to click on the square to the right of that text box.)
  6. On the right side (under FLAGS2 or ARGS2), enter the string -ea in the text box in the same row as the word Run.
  7. Click on OK.

On the command line

Suppose that a class Junk has assert commands in it, and you want those commands to be operative. Then compile the class using the command

javac -g -source 1.5 Junk.java

Now suppose that Garbage is an application, i.e., a Java class that includes a method with the signature

public static void main(String[] args)

and that its execution may lead to assert commands being executed. Then run the application using the command

java -ea Garbage