How to enable Java assertions?

Source: Internet
Author: User

Old Jia asked how to enable assertions. So I searched the internet. Make a record.

Command line:
Java-ea asserttest
-Da is used to disable assertions.

By default, the assertion function is disabled by JVM. To use the assertion function, you must explicitly enable or disable assertion by adding parameters.
In addition, the assertion parameter of assertion allows Java applications to enable the assertion function of some classes or packages. Therefore, it is more complicated to run the assertion function than compile. Here there are two types of parameters to describe:

 

Public class testassert {
Public static void main (string [] ARGs ){
Int x = 1;
Assert x <0;
System. Out. println ("OK ");
}
}

Run ---> run commands ---> select the arguments Tab
In the VM arguments text box, enter "-ea". If "-da" is input, assertions are not allowed.

 

From: http://blog.csdn.net/silentbalanceyh/archive/2009/09/18/4564884.aspx
Compile and run:
[Compilation]
Because assert is a keyword generated by JDK 1.4, you cannot compile a program with assert using the old version of JDK. Therefore, when you compile the code using the javac command, JDK 1.4 or the updated Java compiler must be used. If the assert keyword cannot be identified during compilation, add the compilation parameter-source 1.4. The version number here must be at least 1.4 or later. When you directly use the javac command to compile the source code,-source 1.4 indicates that JDK 1.4 is used to compile the source code. if the version is too low, the Code with the assert keyword cannot be compiled. The content of javac and Java commands will be introduced in a special chapter later.
[Run]
When running a program with an Assert statement, the new classloader class is used. Therefore, you must ensure that the program runs in JDK 1.4 or later or in JRE 1.4 or later. During running, the assertion function is disabled by JVM by default. To use the assertion function, you must explicitly use the Add parameter to enable or disable assertion. In addition, the assertion parameter of assertion allows Java applications to enable the assertion function of some classes or packages. Therefore, it is more complicated to run the assertion function than compile. Here there are two types of parameters to describe:
[1] parameters-ESA and-DSA:
This function enables (disables) the assertion function of the system class. Because the new java system class also uses the assertion statement, if you need to observe their own running status, you need to enable the assertion function, you can use the-ESA parameter to open it, disable it using the-DSA parameter. -The full names of ESA and-DSA are-enablesystemassertions and-disenablesystemassertions. The full names and abbreviations have the same effect.
[2] parameters-ea and-da:
They mean to enable or disable the assertion function of the user class: by using this parameter, you can enable the assertion function of some classes or packages, you can also disable the assertion function of some classes and packages. The parameter used to enable the assertion function is-ea. If no parameter is provided, all user classes are opened. If there is a package name or class name, the assertion function of these classes or packages is enabled. -The full names of EA and-da are-enableassertions and-disenableassertions.
Here we provide a table to demonstrate the usage of the row-based asserted parameter.
Parameter example
-Ea Java-ea opens assertion for all user classes
-Da Java-da: Close assertion of all user classes
-Ea: <classname> JAVA-Ea: assertiondriver: Enable assertion of the assertiondriver class
-Da: <classname> JAVA-da: assertiondriver: Disable assertion of the assertiondriver class
-Ea: <packagename> JAVA-Ea: packagename open the assertion of the packagename package
-Da: <packagename> JAVA-da: packagename: Disable assertion of the packagename package
-Ea:... java-Ea:... open the assertion of the default package (untitled package)
-Da:... java-da:... disable the assertion of the default package (untitled package ).
-Ea: <packagename>... java-Ea: packagename... open the packagename package and assertion of its sub-packages
-Da: <packagename>... java-da: packagename... close the assertion of the packagename package and its sub-packages.
-ESA Java-ESA: Open the assertion of the system class
-DSA Java-DSA: Disable the assertion of the system class
Use Java-DSA: classone: pkgone to disable assertion of the classone class and pkgone package.
In the preceding table, you must note the following:
[1]... indicates the sub-package corresponding to the package. If the system has two packages, pkgone and pkgone. subpkg, pkgone... indicates the two packages.
[2] programming is used to disable or enable assertion. Below is a piece of code to describe this function: programming assertion.
/**
* Use a program to enable the asserted code segment
**/
Class loaded
{
Public void go ()
{
Try
{
Assert false: "loaded. Go ()";
}
Catch (assertionerror error ){
System. Out. println (error );
}
}
}

Public class loaderassertions
{
Public static void main (string ARGs [])
{
Classloader. getsystemclassloader (). setdefaultassertionstatus (true );
New loaded (). Go ();
}
}
Directly run the above Code with the following output:
Java. Lang. assertionerror: loaded. Go ()
In addition to the setdefaultassertionstatus method used by the above Code, the classloader API contains the following methods:
Setdefaultassertionstatus: Used to enable/disable the assertion Function
Setpackageassertionstatus: Used to enable/disable the assertion function of some packages
Setclassassertionstatus: Used to enable/disable the assertion function of some classes
Clearassertionstatus: used to disable the assertion function.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.