Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable the Java keyword assert in Eclipse program-wise?

How can I enable the assert keyword in Eclipse?

public class A {     public static void main(String ... args)     {         System.out.println(1);         assert false;         System.out.println(2);     } } 
like image 414
Siva Kumar Reddy G Avatar asked Jul 10 '12 14:07

Siva Kumar Reddy G


People also ask

How do I enable assert in Java?

To configure assertion options one must use either the -ea or -da command line flags to enable or disable assertions with the command line tool: “java”. For example, “java -ea Assert” where Assert is a java class file. You may also specify a specific class or package as follows.

Why assert is not working in Java?

Assertions can be selectively enabled or disabled at class level or package level. The disable switch is –disableassertions or –da for short. For example, the following command enables assertions in package package1 and disables assertions in class Class1. Assertion should not be used to replace exception handling.

What does assert () do in Java?

Java For Testers An assertion is a statement in Java which ensures the correctness of any assumptions which have been done in the program. When an assertion is executed, it is assumed to be true. If the assertion is false, the JVM will throw an Assertion error. It finds it application primarily in the testing purposes.


2 Answers

To be specific:

  • Go to Run->run configuration
  • select java application in left nav pan.
  • right click and select New.
  • select Arguments tab
  • Add -ea in VM arguments.

enter image description here

like image 179
Priyank Doshi Avatar answered Sep 20 '22 05:09

Priyank Doshi


If anyone wants to enable assertions by default (in contrast to enabling them for just a single run configuration), it is possible with the following steps:

  1. Window (menu bar)
  2. Preferences
  3. Java
  4. Installed JREs
  5. Select your JRE/JDK
  6. Press Edit...
  7. Default VM arguments
  8. Add -ea
like image 28
noone Avatar answered Sep 22 '22 05:09

noone