Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if assertions are enabled

Tags:

java

You can enable/disable assert on the ClassLoader.

But how can you determine if they are already enabled?

(I want to take some code paths that perform expensive checks only if the JVM is invoked with assertions enabled.)

like image 712
Will Avatar asked May 28 '13 09:05

Will


3 Answers

public static boolean areAssertsEnabled() {
  boolean assertsEnabled = false;
  assert assertsEnabled = true; // Intentional side effect!!!
  return assertsEnabled;
}
like image 172
kevin847 Avatar answered Nov 10 '22 12:11

kevin847


boolean assertEnabled = false;

try {
   assert false;
} catch (AssertionError e) {
   assertEnabled = true;
}
like image 27
Ravi K Thapliyal Avatar answered Nov 10 '22 12:11

Ravi K Thapliyal


ManagementFactory.getRuntimeMXBean().getInputArguments().contains("-ea");
like image 2
Eng.Fouad Avatar answered Nov 10 '22 11:11

Eng.Fouad