Java Reference
In-Depth Information
Table 10-2.
List of Arguments That Can Be Passed to -ea and -da Switches
Argument for -ea and -da switches
Description
(no argument)
Enables or disables assertions in all user-defined classes. Note that to
enable/disable assertions in all system classes you need to use
-esa
and
-dsa
switches with no argument, respectively.
packageName...
Note the three dots after the
packageName
. It enables/disables assertions in
the specified
packageName
and any of its subpackages. It can also be used
to enable/disable assertions in system packages.
...
This argument value is three dots. It enables/disables assertions in the
unnamed package in the current working directory.
className
Enables/disables assertions in the specified
className
. It can also be used
to enable/disable assertions in system classes.
The following are examples of using assertion switches with different arguments. All examples assume that you
are enabling assertions when you are running the
com.jdojo.assertion.AssertTest
class. The examples show
you only how to enable assertions. By default, all assertions are disabled.
/* Enable assertions in all system classes */
java -esa com.jdojo.assertion.AssertTest
/* Enable assertions in all user-defined classes */
java -ea com.jdojo.assertion.AssertTest
/* Enable assertions in com.jdojo package and its sub-packages */
java -ea:com.jdojo… com.jdojo.assertion.AssertTest
/* Enable assertions in the unnamed package in the current directory */
java -ea:… com.jdojo.assertion.AssertTest
/* Enable assertions in com.jdojo.assertion.AssertTest class */
java -ea:com.jdojo.assertion.AssertTest com.jdojo.assertion.AssertTest
You can use multiple
-ea
or
-da
switches in one command to achieve finer granularity in enabling/disabling
assertions. All switches are processed from left to right in the order they are specified.
/* Enable assertions in p1 package and all its sub-packages and disable assertion for p1.p2.MyClass
*/
java -ea:p1... -da:p1.p2.MyClass com.jdojo.assertion.AssertTest
■
assertions for a class are enabled or disabled when a class is loaded. the assertion status for a class cannot be
changed after it is set. there is one exception to this rule. if an
assert
statement is executed before a class has been
initialized, the Java runtime executes it as if assertions are enabled. this situation arises when two classes refer to each
other in their
static
initializers by calling the constructors or the methods of another class.
Tip