Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I enable java assertions in Tomcat

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.

like image 850
David Kerr Avatar asked Aug 19 '11 11:08

David Kerr


People also ask

How do I enable Java assertions?

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.

Which command can be use to enable the assertion?

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.

Why assert is not working in Java?

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.

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.


1 Answers

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

like image 117
Bohemian Avatar answered Sep 19 '22 01:09

Bohemian