Sunday, January 12, 2014

How to get the System Properties and System Environment variables by using Java

  1. To get the System properties we can use getProperty() of System class. 
  2. To get all the System properties we can use the getProperties() of System class.

package com.ranga;

/**
 * Created by ranga on 1/12/14.
 */

public class Application {
  public static void main(String[] args) {
    System.out.println("Java Version: "+ System.getProperty("java.version"));
  }
}

Output:
Java Version: 1.6.0_35


Fetching all System properties.
---------------------------------------------------------------
package com.ranga;

import java.util.Map;
import java.util.Properties;

/**
 * Created by ranga on 1/12/14.
 */

public class Application1 {
  public static void main(String[] args) {
    Properties properties = System.getProperties();
    for(Object key : properties.keySet()) {
      System.out.println("Key: "+ key +", Value: "+properties.getProperty(key.toString()));
    }
  }
}

Output:

Key: java.runtime.name, Value: Java(TM) SE Runtime Environment
Key: sun.boot.library.path, Value: /usr/java/jdk1.6.0_35/jre/lib/amd64
Key: java.vm.version, Value: 20.10-b01
Key: java.vm.vendor, Value: Sun Microsystems Inc.
Key: java.vendor.url, Value: http://java.sun.com/
Key: path.separator, Value: :
Key: idea.launcher.port, Value: 7554
Key: java.vm.name, Value: Java HotSpot(TM) 64-Bit Server VM
Key: file.encoding.pkg, Value: sun.io
Key: sun.java.launcher, Value: SUN_STANDARD
Key: user.country, Value: US
Key: sun.os.patch.level, Value: unknown
Key: java.vm.specification.name, Value: Java Virtual Machine Specification
Key: user.dir, Value: /home/ranga/install/ITappCloud/SerializationExample
Key: java.runtime.version, Value: 1.6.0_35-b10
Key: java.awt.graphicsenv, Value: sun.awt.X11GraphicsEnvironment
Key: java.endorsed.dirs, Value: /usr/java/jdk1.6.0_35/jre/lib/endorsed
Key: os.arch, Value: amd64
Key: java.io.tmpdir, Value: /tmp
Key: line.separator, Value:

Key: java.vm.specification.vendor, Value: Sun Microsystems Inc.
Key: os.name, Value: Linux
Key: sun.jnu.encoding, Value: UTF-8
Key: java.library.path, Value: /usr/java/jdk1.6.0_35/jre/lib/amd64/server:/usr/java/jdk1.6.0_35/jre/lib/amd64:/usr/java/jdk1.6.0_35/jre/../lib/amd64:/home/ranga/Softwares/idea-IU-133.193/bin::/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Key: java.specification.name, Value: Java Platform API Specification
Key: java.class.version, Value: 50.0
Key: sun.management.compiler, Value: HotSpot 64-Bit Tiered Compilers
Key: os.version, Value: 3.9.10-100.fc17.x86_64
Key: user.home, Value: /home/ranga
Key: user.timezone, Value:
Key: java.awt.printerjob, Value: sun.print.PSPrinterJob
Key: idea.launcher.bin.path, Value: /home/ranga/Softwares/idea-IU-133.193/bin
Key: file.encoding, Value: UTF-8
Key: java.specification.version, Value: 1.6
Key: java.class.path, Value: /usr/java/jdk1.6.0_35/jre/lib/plugin.jar:/usr/java/jdk1.6.0_35/jre/lib/rt.jar:/usr/java/jdk1.6.0_35/jre/lib/deploy.jar:/usr/java/jdk1.6.0_35/jre/lib/management-agent.jar:/usr/java/jdk1.6.0_35/jre/lib/charsets.jar:/usr/java/jdk1.6.0_35/jre/lib/resources.jar:/usr/java/jdk1.6.0_35/jre/lib/jce.jar:/usr/java/jdk1.6.0_35/jre/lib/javaws.jar:/usr/java/jdk1.6.0_35/jre/lib/jsse.jar:/usr/java/jdk1.6.0_35/jre/lib/ext/sunjce_provider.jar:/usr/java/jdk1.6.0_35/jre/lib/ext/sunpkcs11.jar:/usr/java/jdk1.6.0_35/jre/lib/ext/localedata.jar:/usr/java/jdk1.6.0_35/jre/lib/ext/dnsns.jar:/home/ranga/install/ITappCloud/SerializationExample/target/classes:/home/ranga/Softwares/idea-IU-133.193/lib/idea_rt.jar
Key: user.name, Value: ranga
Key: java.vm.specification.version, Value: 1.0
Key: sun.java.command, Value: com.intellij.rt.execution.application.AppMain com.ranga.MyTest2
Key: java.home, Value: /usr/java/jdk1.6.0_35/jre
Key: sun.arch.data.model, Value: 64
Key: user.language, Value: en
Key: java.specification.vendor, Value: Sun Microsystems Inc.
Key: java.vm.info, Value: mixed mode
Key: java.version, Value: 1.6.0_35
Key: java.ext.dirs, Value: /usr/java/jdk1.6.0_35/jre/lib/ext:/usr/java/packages/lib/ext
Key: sun.boot.class.path, Value: /usr/java/jdk1.6.0_35/jre/lib/resources.jar:/usr/java/jdk1.6.0_35/jre/lib/rt.jar:/usr/java/jdk1.6.0_35/jre/lib/sunrsasign.jar:/usr/java/jdk1.6.0_35/jre/lib/jsse.jar:/usr/java/jdk1.6.0_35/jre/lib/jce.jar:/usr/java/jdk1.6.0_35/jre/lib/charsets.jar:/usr/java/jdk1.6.0_35/jre/lib/modules/jdk.boot.jar:/usr/java/jdk1.6.0_35/jre/classes
Key: java.vendor, Value: Sun Microsystems Inc.
Key: file.separator, Value: /
Key: java.vendor.url.bug, Value: http://java.sun.com/cgi-bin/bugreport.cgi
Key: sun.io.unicode.encoding, Value: UnicodeLittle
Key: sun.cpu.endian, Value: little
Key: sun.desktop, Value: gnome
Key: sun.cpu.isalist, Value:
 

--------------------------------------------------------------------------------------------------------------

To get System environment variables we can use the System.getenv() method.

package com.ranga;

import java.util.Map;

/**
 * Created by ranga on 1/12/14.
 */

public class Application2 {
  public static void main(String[] args) {

    Map<String, String> env = System.getenv();
    for(String key : env.keySet()) {
      System.out.println(key +" --> "+env.get(key));
    }

  }
}

The main difference between System.getProperty() and System.getenv() is
System.getProperty() is for JVM arguments which are passed -DpropName=value to java application launcher(java) and System.getenv() is for Operating System environment variables.


0 comments: