108

I'm running Eclipse Galileo. How do I enable assertions in Eclipse?

As suggested by other sites, I've tried adding the arguments: -ea. I have also tried changing the compiler compliance level to 1.4. Neither of those suggestions worked.

0

5 Answers 5

182
  1. Go to the menu Run, and then to the menu item Run Configurations.

enter image description here

  1. In the left panel, go to Java Application, and then go to Assertions.

  2. In the right panel, choose the tab Arguments.

  3. Under the field for VM arguments, type -ea to enable assertions.

enter image description here

  1. Click on the Apply and then Run button.

To globally set it as the default for everything:

  1. Go to menu Window (if you are on Windows), or go to menu Eclipse (if you are on Mac). For Linux it might be something similar.

  2. Go to Preferences.

  3. Choose Java, and then Installed JREs from the left panel.

  4. Select your JRE, and then click the Edit... button in the right panel.

  5. In the Default VM arguments field, add -ea.

6
  • This doesn't work for Android, you apparently need JUnit Assert.
    – Noumenon
    Commented May 5, 2013 at 23:33
  • 43
    To globally set it as the default for everything, go to Window -> Preferences -> Java / Installed JREs. Select the JRE and click "Edit...". In the "Default VM arguments" field, add "-ea".
    – Silveri
    Commented Oct 28, 2013 at 8:42
  • @nbro I don't have an Assertions item under Java Application. Maybe you meant current run configuration? Commented Jun 3, 2019 at 19:54
  • @JohnMcClane It is possible that different versions of Eclipse have different panels, buttons and menus. If I specifically added that instruction (but it was a long time ago), it is likely the case that I tried it out on my own version (as you can also notice from the pictures, I am using a mac, so it is possible that Eclipse versions are slightly different on other systems).
    – nbro
    Commented Jun 3, 2019 at 19:58
  • 1
    @JohnMcClane Yes, I think you're right. It's possible those were project names. Rather than going to "Assertions", you should go to your project name (I guess).
    – nbro
    Commented Jun 3, 2019 at 20:33
36

If you want to solve this issue globally for all JUnit tests then go to Preferences > Java > JUnit and at the top click the checkbox for "Add 'ea' to VM arguments when creating new JUnit launch configuration" Now Eclipse won't bug you for every new test you want to run. For existing tests you have to remove their's run configurations in Run Configuration > JUnit.

2
  • 5
    Is there any way to add -ea to all existing JUnit launch configurations?
    – mxro
    Commented Dec 12, 2012 at 22:19
  • To be added -ea to existing tests you have to remove run configurations in Run Configuration > JUnit
    – Tharok
    Commented Aug 28, 2014 at 8:09
13

To do this globally for all Java runs, edit the Installed JREs properties.

For example: Windows -> Preferences -> highlight the default JRE -> click Edit... -> In the Default VM arguments input box, enter -ea -> click the finish button.

This worked on Eclipse Kepler SR2.

0
5

On maven projects you can run main method with auto-magic classpath by:

bash> export MAVEN_OPTS="-ea" && mvn exec:java -Dexec.mainClass="com.your.class.with.main"
cmd> set MAVEN_OPTS="-ea" && mvn exec:java -Dexec.mainClass="com.your.class.with.main"

In linux/eclipse you can add terminal window with bash for more convenient use.

4

You want to enable assertions for an application you're running from Eclipse? I usually just add -ea to the command line parameters in the "Run As" profile.

1
  • 1
    Yeah, that's how I usually do it too when I'm running from the command line, which is why I thought adding those VM arguments in the preferences would work. Thanks for the input. In confirms I wasn't crazy for thinking that should do it.. Commented Apr 1, 2011 at 4:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.