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

JavaEclipseAssertAssertions

Java Problem Overview


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);
    }
}

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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

Solution 3 - Java

  1. Form the menu bar, select Run -> Run Configurations....
  2. Select Arguments tab.
  3. Add -ea to VM arguments.
  4. Click Apply.
  5. Click Run.

Solution 4 - Java

You need to go to run configurations and add vm arguments as "-enableassertions" (or) "-ea"

After that when you run code with assert statement, you will see assert in action.

Solution 5 - Java

Java introduced the assert keyword, so the way to enable source-level support is to make sure that Eclipse's Java compliance level is 1.4 or higher. (The chances are that the compliance level is already higher than that ...)

To cause a Java application launched from Eclipsed to run with assertion checking enabled, add the "-ea" argument to the VM arguments in the launcher configuration's "Arguments" tab.

Solution 6 - Java

In case someone uses IDEA, -ea is enabled by default in some IDEA versions, but disabled in others. To manually configure it, it is under run -> edit configuration -> vm option

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSiva Kumar Reddy GView Question on Stackoverflow
Solution 1 - JavaPriyank DoshiView Answer on Stackoverflow
Solution 2 - JavanooneView Answer on Stackoverflow
Solution 3 - JavaEng.FouadView Answer on Stackoverflow
Solution 4 - JavakosaView Answer on Stackoverflow
Solution 5 - JavaStephen CView Answer on Stackoverflow
Solution 6 - JavaTiinaView Answer on Stackoverflow