65

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);
    }
}
Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
Siva Kumar Reddy G
  • 1,274
  • 3
  • 18
  • 32
  • 7
    The JVM takes argument `-enableassertions`, or `-ea`. Not sure where that is specified in Eclipse though. – hmjd Jul 10 '12 at 14:11

6 Answers6

106

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

Priyank Doshi
  • 12,895
  • 18
  • 59
  • 82
49

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
noone
  • 19,520
  • 5
  • 61
  • 76
  • 3
    This is a very useful supplement; I can't believe I've been adding `-ea` to every run configuration (specifically while debugging) as assertions are such a handy, yet underused, feature during development. – Haravikk Jun 15 '14 at 16:39
9
  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.
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
2

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.

kosa
  • 65,990
  • 13
  • 130
  • 167
2

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

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

Tiina
  • 4,285
  • 7
  • 44
  • 73