I would like to use the Java assert
keyword in my Spring web-app, primarily on my domain classes (checking invariants within constructors), but I cannot see how to enable runtime assertion checking.
For a normal java application, I would use the -ea
switch with the java command, but I don't see how I can do that with Tomcat, etc.
Is this the recommended thing, or should I use the "Assert" class from the Spring framework? I'd rather keep my domain classes free of Spring dependencies, however.
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.
You can use the command-line arguments –ea (for enabling assertions) and –da (for disabling assertions) with other options for classes, packages, and system classes when running the program.
assert is not a function, its a keyword. Its a build in feature that must be enabled before it can be used. To enable this feature you have to add the command "-ea" to the argument list when initializing your JVM.
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.
For tomcat, add -ea
to JAVA_OPTS
(all java processes started), or even better to CATALINA_OPTS
(only to web app engine).
Read the files catalina.bat
or catalina.sh
for more information on JAVA_OPTS
and CATALINA_OPTS
.
Edited: Thanks to comments from @Joachim
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With