Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Beginner
May 8, 2002

An Easy Way to Check Boolean System Properties


Suppose you want to pass a Boolean system property, -DDEBUG=true or -DDEBUG=false, from the command line. Normally, you'd probably use the following in source code to retrieve it:
 
boolean b = Boolean.valueOf
(System.getProperty("DEBUG")).booleanValue();
if(b) {
    //do something;
}

or just treat it as a string system property:
if(System.getProperty("DEBUG").equalsIgnoreCase("true")) {
    //do something;
}

The following code offers an easier way to do the same thing:
 
public class GetBoolean {
    public static void main(String args[]) 
throws Exception {
        if(Boolean.getBoolean("DEBUG")) {
            System.out.println("Debug is on");
        } else {
            System.out.println("Debug is off");
        }
    }
}

The GetBoolean class is tested under JDK1.3.1 with the following output:
 
D:\foo>java -DDEBUG=true GetBoolean
Debug is on

D:\foo>java -DDEBUG=false GetBoolean
Debug is off

D:\foo>java -DDEBUG=TRue GetBoolean
Debug is on

D:\foo>java -DDEBUG=Yes GetBoolean
Debug is off

D:\foo>java GetBoolean
Debug is off
Linghong Ji
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date