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.