register
other register

Monday, July 28, 2008

Set System Properties in Java

You can set system properties by using -D in the java command line:
java -Dfile_properties=file.properties HelloWorld

==========================================

String file_properties = System.getProperty("file_properties");

// load file.properties file which sets the files and directories
Properties properties = new Properties();
try {
//properties.load(new FileInputStream("file.properties"));
properties.load(new FileInputStream(System.getProperty("file_properties")));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1)
{
// TODO Auto-generated catch block e1.printStackTrace();
}

// get properties File inputDir = new File(properties.getProperty("inputDir"));

==========================================

In Eclipse, you can set system properties in:

1. Right click the java program, choose Run As --> Open Run Dialog
2. Select Arguments tab
3. In "VM argument", write your system properties settings. (i.e. -Dfile_properties=file.properties).

No comments: