Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java System properties

farenda 2015-09-07 0

Problem:

How to read Java System Properties? In the following example we show how to read single System property, list all, and use default when property is not defined.

Solution:

java.lang.System has a couple of methods that help to read System properties. They are:

  • Properties getProperties() Loads all system properties.
  • String getProperty(String key) Returns value of specified system property or null.
  • String getProperty(String key, String def) Returns value of specified system property or default value when not found.

All three methods are demonstrated in the following example:

package com.farenda.java.lang;

import java.util.Map.Entry;

public class SystemGetProperties {

    public static void main(String[] args) {
        // Get property value or null:
        System.out.println("Java version: "
                + System.getProperty("java.version"));

        System.out.println("Unknown property: "
                + System.getProperty("unknown.property"));

        // Use default if not found:
        System.out.println("\nConfig location: "
                + System.getProperty("config.location", "/tmp/config"));

        // List all system properties:
        System.out.println("\nSystem properties:");
        for (Entry<Object,Object> property : System.getProperties().entrySet()) {
            System.out.printf("%s: %s%n", property.getKey(), property.getValue());
        }
    }
}

Truncated result of running the above example:

Java version: 1.8.0_45
Unknown property: null

Config location: /tmp/config

System properties:
java.runtime.name: Java(TM) SE Runtime Environment
sun.boot.library.path: /usr/lib/jvm/java-8-oracle/jre/lib/amd64
java.vm.version: 25.45-b02
java.vm.vendor: Oracle Corporation
java.vendor.url: https://java.oracle.com/
path.separator: :
idea.launcher.port: 7533
java.vm.name: Java HotSpot(TM) 64-Bit Server VM
[...]

In the following posts we’ll dig dipper into Properties, so stay tuned! :-)

Share with the World!
Categories Java Tags java, java-basics
Previous: Java null check
Next: Java list Properties

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored