Monday, January 16, 2017

How to enable or disable Assertion in Java

The good thing about Assertion in Java is that you can enable or disable Assertion at runtime. The designer of java are really very thoughtful, they have thought of many situations while designing java and its API. one of the gems is the ability to enable or disable assertion in runtime without any change on code. In last article we see how Assertion works in Java and now we will see some step by step guide on how to disable or enable Assertion in Java By default assertions in Java are disabled at runtime and JVM provides following options to deal with assertion in Java :

Enable or disable Assertion in Java

--ea[:...|:] or enableassertions  (to enable assertion at a particular package and class level)
--da[:...|:] or disableassertions (to disable assertion on a package or class level)
--esa or enablesystemassertions  (for enabling system assertion)
--dsa or disablesystemassertions (for disabling system assertion)

1) if we use -enableassertions or -disableassertions without arguments it enables or disable assertion in all classes except System classes

2) if we use -ea or -da with "packageName..." it enables or disable assertions in the named package and any sub package.

3) -enableassersions with "..." only enable assertions in the unnamed package in current working directory.

4) -enableassertions with "classname" only enable assertion in the named class in Java.

Example of Enable and Disable Assertion in Java

how to enable and disable assertions in Java example guideHere is an example of enabling assertion in java: in below example assertions will be enabled for package  "com.onlinestockbroker.electronicTrading" and it sub package.

java -ea:com.onlinestockbroker.electronicTrading... EquityStocks

and here is another example of disabling Assertion in the same class

java -da:com.onlinestockbroker.electronicTrading... EquityStocks

That’s all on how to enable and disable Java Assertion. Important point is that you don’t need to change anything on code and can disable assertion at runtime with just changing some JVM options, which gives you the flexibility to run the same code on development and production environment. While in development you can enable assertion, you can disable assertion in production with just JVM switch.

Further Learning
Java Fundamentals Part 1,2
Java Fundamentals: The Java Language
Core Java, Volume II--Advanced Features

Java Tutorials you may like


Preeti said...

Can you please let us know how to enable or disable assertions on Netbeans and Eclipse IDE ? I have a project which uses Assertions in various part of code and I want to enable them while working in Eclipse IDE.

Anand VijayaKumar said...

More details on what assertions are and how they are useful can be found by clicking here


Anonymous said...

How can we enable assertion in Eclipse ? I want to keep assertions enable by default for every Java project, anybody knows Eclipse settings for that?

Post a Comment