System Properties
* Getting and Setting the Value of a System Property
* Listing All System Properties
* Setting the Value of a System Property from the Command Line
Getting and Setting the Value of a System Property
// Get a system property
String dir = System.getProperty("user.dir");
// Set a system property
String previousValue = System.setProperty("application.property", "newValue");
--------------------------------------------------------------------------------
Listing All System Properties
// Get all system properties
Properties props = System.getProperties();
// Enumerate all system properties
Enumeration enum = props.propertyNames();
for (; enum.hasMoreElements(); )
{
// Get property name
String propName = (String)enum.nextElement();
// Get property value
String propValue = (String)props.get(propName);
}
---------------------------------------------------------------------------------
Setting the Value of a System Property from the Command Line
A system property can be set or overridden by specifying the -D option to the java command when running your program.
java -Dmy.prop="my value" MyApp
// Get the value of the system property
String prop = System.getProperty("my.prop");
// my value
No comments:
Post a Comment