java.lang.Properties object holds properties information of current runtime environment. Properties class is an implementation of HashTable, it contains information in the form of Key/Value pairs. System class has five methods to play with system properties.
These methods are :
Getter methods
System.getProperties();
System.getProperty(String key);
System.getProperty(String key, String def);
Setter methods
These methods are :
Getter methods
System.getProperties();
System.getProperty(String key);
System.getProperty(String key, String def);
Setter methods
System.setProperties(Properties properties);
System.setProperty(String key,String value);
To list the system properties save the below code with name ListSystemProperties.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package com.trickyjava.general; import java.util.Map.Entry; import java.util.Properties; public class ListSystemProperties { public static void main(String[] args) { Properties properties = System.getProperties(); for (Entry<Object, Object> entry : properties.entrySet()) { System.out.println(entry.getKey() + " : " + entry.getValue()); } } } |
And the output of above code will like:
java.runtime.name : OpenJDK Runtime Environment sun.boot.library.path : /usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386 java.vm.version : 24.51-b03 java.vm.vendor : Oracle Corporation java.vendor.url : http://java.oracle.com/ path.separator : : java.vm.name : OpenJDK Server VM file.encoding.pkg : sun.io user.country : IN sun.java.launcher : SUN_STANDARD sun.os.patch.level : unknown java.vm.specification.name : Java Virtual Machine Specification user.dir : /home/trickyJavaAdmin/WORK/WORKSPACE/TrickyJavaCode java.runtime.version : 1.7.0_55-b14 java.awt.graphicsenv : sun.awt.X11GraphicsEnvironment java.endorsed.dirs : /usr/lib/jvm/java-7-openjdk-i386/jre/lib/endorsed os.arch : i386 java.io.tmpdir : /tmp line.separator : java.vm.specification.vendor : Oracle Corporation os.name : Linux sun.jnu.encoding : UTF-8 java.library.path : /usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/server:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386:/usr/lib/jvm/java-7-openjdk-i386/jre/../lib/i386:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386::/usr/java/packages/lib/i386:/usr/lib/i386-linux-gnu/jni:/lib/i386-linux-gnu:/usr/lib/i386-linux-gnu:/usr/lib/jni:/lib:/usr/lib java.specification.name : Java Platform API Specification java.class.version : 51.0 sun.management.compiler : HotSpot Tiered Compilers os.version : 3.8.0-34-generic user.home : /home/trickyJavaAdmin user.timezone : java.awt.printerjob : sun.print.PSPrinterJob file.encoding : UTF-8 java.specification.version : 1.7 java.class.path : /home/trickyJavaAdmin/WORK/WORKSPACE/TestJava/bin:/home/trickyJavaAdmin/Downloads/google-api-translate-java-0.97.jar user.name : trickyJavaAdmin java.vm.specification.version : 1.7 sun.java.command : com.trickyjava.general.ListSystemProperties java.home : /usr/lib/jvm/java-7-openjdk-i386/jre sun.arch.data.model : 32 user.language : en java.specification.vendor : Oracle Corporation awt.toolkit : sun.awt.X11.XToolkit java.vm.info : mixed mode java.version : 1.7.0_55 java.ext.dirs : /usr/lib/jvm/java-7-openjdk-i386/jre/lib/ext:/usr/java/packages/lib/ext sun.boot.class.path : /usr/lib/jvm/java-7-openjdk-i386/jre/lib/resources.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/rt.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/sunrsasign.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/jsse.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/jce.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/charsets.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/netx.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/plugin.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/rhino.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/jfr.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/classes java.vendor : Oracle Corporation file.separator : / java.vendor.url.bug : http://bugreport.sun.com/bugreport/ sun.io.unicode.encoding : UnicodeLittle sun.cpu.endian : little sun.desktop : gnome sun.cpu.isalist :
If you want sorted( by property key) list then replace the code main method with the below code
1 2 3 4 5 | Properties properties = System.getProperties(); Map<Object, Object> propertiesMap = new TreeMap<>(properties); for (Entry<Object, Object> entry : propertiesMap.entrySet()) { System.out.println(entry.getKey() + " : " + entry.getValue()); } |
To set the System properties save the below code with name SetSystemProperties.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package com.trickyjava.general; public class SetSystemProperties { public static void main(String[] args) { System.setProperty("movies.home", "/home/trickyJavaAdmin/movies"); System.setProperty("movies.home.hollywood", "/home/trickyJavaAdmin/movies/hollywoodmovies"); System.setProperty("movies.home.bollywood", "/home/trickyJavaAdmin/movies/bollywoodmovies"); System.out.println("Movies Directory : "+System.getProperty("movies.home")); System.out.println("Hollywood Movies Directory : "+System.getProperty("movies.home.hollywood")); System.out.println("Bollywood Movies Directory : "+System.getProperty("movies.home.bollywood")); } } |
Output of above code :
Movies Directory : /home/trickyJavaAdmin/movies Hollywood Movies Directory : /home/trickyJavaAdmin/movies/hollywoodmovies Bollywood Movies Directory : /home/trickyJavaAdmin/movies/bollywoodmovies
No comments:
Post a Comment