Sunday 12 January 2020

System getProperties vs getenv

System.getenv()
'System.getenv' method return the system environment properties. If the system does not support environment variables, an empty map is returned.

App.java
package com.sample.app;

import java.util.Map;

public class App {

 public static void main(String args[]) {

  Map<String, String> properties = System.getenv();

  for (String key : properties.keySet()) {
   System.out.println(key + " = " + properties.get(key));
  }
 }

}

Output
PATH = /usr/bin:/bin:/usr/sbin:/sbin
SHELL = /bin/bash
SQLITE_EXEMPT_PATH_FROM_VNODE_GUARDS = /Users/krishna/Library/WebKit/Databases
USER = krishna
JAVA_MAIN_CLASS_62467 = com.sample.app.App
APP_ICON_5033 = ../Resources/Eclipse.icns
TMPDIR = /var/folders/1c/tb7sr9q90mb62pqngbt4l56h5l9cz7/T/]
LOGNAME = krishna
XPC_SERVICE_NAME = org.eclipse.platform.ide.11092
JAVA_STARTED_ON_FIRST_THREAD_5033 = 1
HOME = /Users/Krishna

System.properties
This method return Java specific properties. You can even set new java system properties at command line while running the application.

App.java    
package com.sample.app;

import java.util.Enumeration;
import java.util.Properties;

public class App {

 public static void main(String args[]) {

  Properties properties = System.getProperties();

  Enumeration<Object> props = properties.keys();

  while (props.hasMoreElements()) {
   Object key = props.nextElement();
   Object value = properties.get(key);
   System.out.println(key + " = " + value);
  }

 }

}

Output
java.runtime.name = Java(TM) SE Runtime Environment
sun.boot.library.path = /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib
java.vm.version = 25.221-b11
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 = IN
sun.java.launcher = SUN_STANDARD
sun.os.patch.level = unknown
java.vm.specification.name = Java Virtual Machine Specification
user.dir = /Users/krishna/eclipse-workspace/sof
java.runtime.version = 1.8.0_221-b11
java.awt.graphicsenv = sun.awt.CGraphicsEnvironment
java.endorsed.dirs = /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/endorsed
os.arch = x86_64
java.io.tmpdir = /var/folders/1c/tb7sr9q90mb62pqngbt4l56h5l9cz7/T/
line.separator = 

java.vm.specification.vendor = Oracle Corporation
os.name = Mac OS X
sun.jnu.encoding = UTF-8
java.library.path = /Users/krishna/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.14.6
user.home = /Users/krishna
user.timezone = 
java.awt.printerjob = sun.lwawt.macosx.CPrinterJob
file.encoding = UTF-8
java.specification.version = 1.8
java.class.path = /Users/krishna/eclipse-workspace/sof/target/classes:/Users/krishna/.m2/repository/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar:/Users/krishna/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.6.3/jackson-core-2.6.3.jar:/Users/krishna/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.6.3/jackson-databind-2.6.3.jar:/Users/krishna/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.6.0/jackson-annotations-2.6.0.jar:/Users/krishna/.m2/repository/com/owlike/genson/1.6/genson-1.6.jar:/Users/krishna/.m2/repository/org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar:/Users/krishna/.m2/repository/org/apache/commons/commons-text/1.8/commons-text-1.8.jar:/Users/krishna/.m2/repository/org/springframework/spring-web/5.2.2.RELEASE/spring-web-5.2.2.RELEASE.jar:/Users/krishna/.m2/repository/org/springframework/spring-beans/5.2.2.RELEASE/spring-beans-5.2.2.RELEASE.jar:/Users/krishna/.m2/repository/org/springframework/spring-core/5.2.2.RELEASE/spring-core-5.2.2.RELEASE.jar:/Users/krishna/.m2/repository/org/springframework/spring-jcl/5.2.2.RELEASE/spring-jcl-5.2.2.RELEASE.jar
user.name = krishna
java.vm.specification.version = 1.8
sun.java.command = com.sample.app.App
java.home = /Library/Java/JavaVirtualMachines/jdk1.8.0_221.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_221
java.ext.dirs = /Users/krishna/Library/Java/Extensions:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.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_221.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/sunrsasign.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.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
sun.cpu.isalist =

You can even set the system properties while launching the application from command line.

Example
java -DpropertyName1=value1 -DpropertyName2=value2... {ApplicationName}

Example
java -Dname=Krishna -DnoOfThreads=20 App

App.java
public class App {

 public static void main(String args[]) {

  String noOfThreads = System.getProperty("noOfThreads");
  String name = System.getProperty("name");
  
  System.out.println("Number Of Thread : " + noOfThreads);
  System.out.println("Name : " + name);
 }

}

Compile and run the application by passing vm arguments.

$javac App.java
$java -Dname=Krishna -DnoOfThreads=20 App
Number Of Thread : 20
Name : Krishna

How to pass vm arguments in Eclipse?
Right click on Application.


Run As -> Run Configurations…

Go to Arguments tab.

Add below statement in VM arguments: tab.
-Dname=Krishna -DnoOfThreads=20
Click on Run button to run the application.


You may like

No comments:

Post a Comment