Thursday 31 December 2015

Reading and Writing system properties in java

In this post, I am going to explain how to read and write System properties in Java. This may not be important interview question, but it is good to know about, what is system property, how can you work with them in Java.

System properties are maintained in the form of Key, Value pairs. Following table summarizes the methods provided by System class to work with System properties.

Method
Description
public static String clearProperty(String key)
Remove the property associated with given key.
public static Properties getProperties()
Return all the system properties.
public static String getProperty(String key)
Get the system property associated with given key.
public static String getProperty(String key, String def)
Return the system property associated with given key. Return def if there is no property associated with this key.
public static void setProperties(Properties props)
Sets the system properties to the Properties argument.
public static String setProperty(String key, String value)
Sets the system property indicated by the specified key.

Note
Access to these system properties are restricted by Security Manager, checkPermission method is called before working on all these methods.

Following are the various system properties.

System properties related to Java
Key
Description
java.version   
Java Runtime Environment version
java.vendor
Java Runtime Environment vendor
java.vendor.url
Java vendor URL
java.home  
Java installation directory
java.vm.specification.version  
Java Virtual Machine specification version
java.vm.specification.vendor   
Java Virtual Machine specification vendor
java.vm.specification.name 
Java Virtual Machine specification name
java.vm.version
Java Virtual Machine implementation version
java.vm.vendor 
Java Virtual Machine implementation vendor
java.vm.name   
Java Virtual Machine implementation name
java.specification.version 
Java Runtime Environment specification version
java.specification.vendor  
Java Runtime Environment specification vendor
java.specification.name
Java Runtime Environment specification name
java.class.version 
Java class format version number
java.class.path
Java class path
java.library.path  
List of paths to search when loading libraries
java.io.tmpdir 
Default temp file path
java.compiler  
Name of JIT compiler to use

System properties related to operating system
Key
Description
os.name
Operating system name
os.arch
Operating system architecture
os.version 
Operating system version

System properties related to files
Key
Description
file.separator
File separator ("/" on UNIX)
path.separator
Path separator (":" on UNIX)
line.separator 
Line separator ("\n" on UNIX)

System properties related to user
Key
Description
user.name  
User's account name
user.home  
User's home directory
user.dir   
User's current working directory


Following program print all the system properties to console.

import java.util.Properties;
import java.util.Set;

public class SystemPropertyDemo {
    public static void main(String args[]) {
        Properties systemProps = System.getProperties();

        Set<Object> keys = systemProps.keySet();

        for (Object key : keys) {
            System.out.println(key + ":\t" + systemProps.get(key));
        }
    }
}


Output
java.runtime.name:  Java(TM) SE Runtime Environment
sun.boot.library.path:  /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib
java.vm.version:    25.45-b02
user.country.format:    IN
gopherProxySet: false
java.vm.vendor: Oracle Corporation
java.vendor.url:    http://java.oracle.com/
path.separator: :
java.vm.name:   Java HotSpot(TM) 64-Bit Server VM
file.encoding.pkg:  sun.io
user.country:   US
sun.java.launcher:  SUN_STANDARD
sun.os.patch.level: unknown
java.vm.specification.name: Java Virtual Machine Specification
user.dir:   /Users/harikrishna_gurram/Documents/workspace1/miscellaneous
java.runtime.version:   1.8.0_45-b14
java.awt.graphicsenv:   sun.awt.CGraphicsEnvironment
java.endorsed.dirs: /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/endorsed
os.arch:    x86_64
java.io.tmpdir: /var/folders/1x/9q4yr4k15csf564llfcz3c1h7n6wlv/T/
line.separator: 

java.vm.specification.vendor:   Oracle Corporation
os.name:    Mac OS X
sun.jnu.encoding:   UTF-8
java.library.path:  /Users/harikrishna_gurram/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
java.specification.name:    Java Platform API Specification
java.class.version: 52.0
sun.management.compiler:    HotSpot 64-Bit Tiered Compilers
os.version: 10.9.5
http.nonProxyHosts: local|*.local|169.254/16|*.169.254/16
user.home:  /Users/harikrishna_gurram
user.timezone:  Asia/Kolkata
java.awt.printerjob:    sun.lwawt.macosx.CPrinterJob
file.encoding:  UTF-8
java.specification.version: 1.8
java.class.path:    /Users/harikrishna_gurram/Documents/workspace1/miscellaneous/bin
user.name:  harikrishna_gurram
java.vm.specification.version:  1.8
sun.java.command:   SystemPropertyDemo
java.home:  /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre
sun.arch.data.model:    64
user.language:  en
java.specification.vendor:  Oracle Corporation
awt.toolkit:    sun.lwawt.macosx.LWCToolkit
java.vm.info:   mixed mode
java.version:   1.8.0_45
java.ext.dirs:  /Users/harikrishna_gurram/Library/Java/Extensions:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/ext:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java
sun.boot.class.path:    /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/sunrsasign.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/classes
java.vendor:    Oracle Corporation
file.separator: /
java.vendor.url.bug:    http://bugreport.sun.com/bugreport/
sun.io.unicode.encoding:    UnicodeBig
sun.cpu.endian: little
socksNonProxyHosts: local|*.local|169.254/16|*.169.254/16
ftp.nonProxyHosts:  local|*.local|169.254/16|*.169.254/16
sun.cpu.isalist:    

You may like

References




                                                 Home

No comments:

Post a Comment