Easily process command line parameters

Source: Internet
Author: User

 


You can easily use java standard transfer flag to obtain a simple and powerful command line application. In fact, the passed sign is not only valid in the main method.

The java command directly passes the-D flag to the Properties object of the System class. This allows the System. getProperty method to detect this flag.

Run the following code through java-Dflag = jack to output "jack" on the screen ".

Public class ShowFlag {

Static public void main (String [] args ){
String flag = System. getProperty ("flag ");
System. err. println (flag );
}

}
To view all the attributes, run the following code:

Import java. util. Properties;

Public class ShowAll {

Static public void main (String [] args ){
Properties sysprops = System. getProperties ();
Sysprops. list (System. err );
}

}
When a new system attribute is passed, the domain name is usually in reverse order. For example:

Java-Dcom. generationjava. someproduct. color = green
Rather:
Java-Dcolor = green


This technique prevents conflicts between your tag and other system attributes that are passed in.

This is just an example of a simple class. A proper parameter processor is required for a complete Java command line application. This parameter processor can help verify whether a specific tag is allowed. It also allows automatic conversion of a simple character tag and value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.