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. Hover over 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 button.
  5. On the form that appears, go to the row labeled Run and in the text box in the second column (labeled FLAGS2 or ARGS2), enter the string -ea (or, equivalently, -enableassertions). However, before you can enter text into the box, you'll have to click on the smaller box to its right, which contains a small black box within it. Doing so should make the small black box disappear, at which point the text box to its left will become receptive to text being entered into it.
  6. Click on Apply and then OK.

On the command line

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 (either within it or within some class of which it is a client, directly or indirectly). Then run the application using the command

java -ea Garbage