tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Java > System > System Properties

System Properties 

This example shows how to access system default properties and user defined properties while starting the appliction in console.

File Name  :  
com/bethecoder/tutorials/system/SystemPropertiesTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.system;

public class SystemPropertiesTest {

   public static void main(String[] args) {

     //Default properties
     System.out.println("OS : " + getOS());
     System.out.println("Cur Directory : " + getCurDirectory());
     System.out.println("Class Path : " + getClassPath());
     System.out.println("Library Path : " + getLibraryPath());

     //Get user defined properties passed in command line.
     System.out.println("userDef1 : " + System.getProperty("my.prop1"));
     System.out.println("userDef2 : " + System.getProperty("my.prop2"));
     
     //Change property
     System.setProperty("my.prop1""SIX");
     System.out.println("userDef1 : " + System.getProperty("my.prop1"));
     
     //Clear property
     System.clearProperty("my.prop1");
     System.out.println("userDef1 : " + System.getProperty("my.prop1"));

     //List all properties
     System.out.println();
     System.getProperties().list(System.out);   
  }
   
   public static String getOS() {
     return System.getProperty("os.name");
   }
   
   public static String getCurDirectory() {
     return System.getProperty("user.dir");
   }
   
   public static String getClassPath() {
     return System.getProperty("user.dir");
   }

   public static String getLibraryPath() {
     return System.getProperty("java.library.path");
   }
   
}
   

In console we can invoke the application as shown below,

C:\Test>java -Dmy.prop1=ONE -Dmy.prop2=TWO com.bethecoder.tutorials.system.SystemPropertiesTest

It gives the following output,

File Name  :  
source/com/bethecoder/tutorials/system/props.txt 
OS : Windows XP
Cur Directory : C:\Test
Library Path : C:\WINDOWS\system32;.;C:\WINDOWS\Sun\Java\bin;
C:\WINDOWS\system32;C:\WINDOWS;D:\Program Files\wamp\mysql\bin;
userDef1 : ONE
userDef2 : TWO
userDef1 : SIX
userDef1 : null


-- listing properties --

java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=C:\Program Files\Java\jre1.6.0_02\bin
java.vm.version=1.6.0_02-b05
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=;
java.vm.name=Java HotSpot(TM) Client VM
file.encoding.pkg=sun.io
user.country=US
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=Service Pack 2
java.vm.specification.name=Java Virtual Machine Specification
user.dir=C:\Test
java.runtime.version=1.6.0_02-b05
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=C:\Program Files\Java\jre1.6.0_02\lib...
os.arch=x86
java.io.tmpdir=C:\DOCUME~1\sudhakar\LOCALS~1\Temp\
line.separator=

java.vm.specification.vendor=Sun Microsystems Inc.
user.variant=
os.name=Windows XP
sun.jnu.encoding=Cp1252
java.library.path=C:\WINDOWS\system32;.;C:\WINDOWS\Sun\...
java.specification.name=Java Platform API Specification
java.class.version=50.0
sun.management.compiler=HotSpot Client Compiler
os.version=5.1
user.home=C:\Documents and Settings\sudhakar
user.timezone=
java.awt.printerjob=sun.awt.windows.WPrinterJob
file.encoding=Cp1252
java.specification.version=1.6
user.name=sudhakar
java.class.path=.
java.vm.specification.version=1.0
sun.arch.data.model=32
java.home=C:\Program Files\Java\jre1.6.0_02
java.specification.vendor=Sun Microsystems Inc.
user.language=en
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode
java.version=1.6.0_02
my.prop2=TWO
java.ext.dirs=C:\Program Files\Java\jre1.6.0_02\lib...
sun.boot.class.path=C:\Program Files\Java\jre1.6.0_02\lib...
java.vendor=Sun Microsystems Inc.
file.separator=\
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport...
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.desktop=windows
sun.cpu.isalist=


                    Print

   



Books Recommendation

 
 
bl  br