Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable assertions in Java Shell

Tags:

java

java-9

C:\Users\Malvolio>jshell
|  Welcome to JShell -- Version 9.0.1
|  For an introduction type: /help intro

jshell> assert(false)

jshell> 

How can I make the Java Shell throw an AssertionError when an assert fails?

like image 908
DodgyCodeException Avatar asked Dec 11 '17 14:12

DodgyCodeException


People also ask

How do I enable assertions 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.

Are assertions enabled by default Java?

Assertions are not enabled by default, because they should be always fullfilled. You enable them to test for that, but then you "know" (as far as you can know) that they are not violated. So you don't need to check the conditions (which might be performance intensive) every time in production code.

Is assertion enabled by default?

Enabling and Disabling AssertionsBy default, assertions are disabled at runtime. Two command-line switches allow you to selectively enable or disable assertions. Enables or disables assertions in all classes except system classes.


1 Answers

Start it with the command option:

jshell -R -ea

enter image description here

like image 182
Eugene Avatar answered Sep 24 '22 16:09

Eugene