Usage:
The JVM parameter “enableassertions” or “ea” (short command) is used to enable assertion mechanism in JVM.
Since:
Starting from JDK 1.4
Syntax:
- java -enableassertions:[<package name> … |<class name> ]
- java -ea:[<package name> … | <class name> ]
Examples:
An example to enable assertions for each case:
- For a class, “java -enableassertions (or -ea) MainClass”
- For only package “java.package” and it’s subpackages, “java -enableassertions (or -ea):com.java.package... MainClass”
- with no arguments, “java -enableassertions (or -ea)” to enables assertions in all packages and classes.
Description:
The assert keywork was introduced in Java since the version 1.4 and it’s a programming language feature to check if a conditional expression evaluates to true when the program is running. They are useful in the testing and development process and are typically omitted for production code. You can use them for example to check if the argument in a function should satisfy a condition or the command line flags contains a certain argument…. If the condition is not verified and the assertion flag “enableassertions” or “ea” is enabled then JVM will throw an “AssertionError”.
Thus, this parameter is used to turn on the assertion mechanism in JVM, with no arguments, it will enable assertions for all packages. If you want to enable assertions only on the main class, you will need to add the parameter followed by the main class and if you want to turn it on for a package and its subpackages, you should use the parameter appended to it the name of the package and the three dots “…” and the main class afterwards. Here are the options of the parameter:
- ..: Enables assertions in the named package and any subpackages.
- ...: Enables assertions in the unnamed package in the current working directory.
- className: Enables assertions in the named class.
Default Value:
By default, assertions are disabled.
Errors:
- If the package or main class doesn’t exist.
- An exception “AssertionError” is thrown while running the program if an assertion is not satisfied
Arguments related:
disableassertions
Related Posts:
Edit your Comment