Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Debugging in Java - Setting a debugMode flag

Apologies for a noobish question.

I am developing a JavaFX application.

I want to set a 'Global' boolean type variable 'DebugMode', Such that when a custom(I mean customized, or made by myself) exception is thrown, the getMessage() function can check the DebugMode variable to see if the DebugMode is set.

If DebugMode is 'true', then the exception can give detailed message for debugging, Or else just user friendly messages for the end users.

Is it possible in java, just like $GLOBAL variable in PHP? Or is there any alternative way for giving two different exception messages for developer and end user?

like image 791
itsfarseen Avatar asked Mar 24 '23 04:03


1 Answers

Go for -D argument (JVM Properties). This means, that when I start the application from command-line. I can do something like,

     java Hello -DdetailedDebugMode=true

And in the java code I can do something like:

  printError(Throwable t) {
      if(System.getProperties("detailedDebugMode") != null && "true".equalsIgnoreCase(System.getProperties("detailedDebugMode"))) {
          LOG.error("Here is the message", e);
      } else {
          LOG.error("Here is the message only");
like image 55
Himanshu Bhardwaj Avatar answered Apr 02 '23 11:04

Himanshu Bhardwaj