[ Back | Previous | Next ]

Adding own properties to system properties routine

Package:
java.util.*
Product:
JDK
Release:
1.0.2
Related Links:
Calendar
EventObject
Hashtable
Locale
Properties
PropertyResourceBundle
TimeZone
Comment:
The file terras.props holds the properties for the application. We will load the properties file into a setup properties instance and copy these into the System.getProperties() properties list.

   private void loadProperties() 
   {
      System.out.println("Loading properties...");
      try 
      {
         /* Scanning for properties file */

         File props = new File( "terras.props" );
         if ( ! props.exists() )
         {
            throw new FileNotFoundException("cannot find 'terras.props' property file");
         }


         /* Loading the setup properties */

         Properties setup = new Properties();
         setup.load( new FileInputStream( props ) );
         system = System.getProperties();

         /* adding setup properties to the system properties */

         for (Enumeration e=setup.keys(); e.hasMoreElements();) {
            Object name = (Object)e.nextElement();
            Object value = (Object)setup.get(name);
            system.put( name, value );
            //System.out.println(name+"="+value);
         }
         

      } catch (Exception e) {
         System.out.println( e.getMessage() );
         e.printStackTrace();
         System.exit(0);
      }
   }
1