• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer
  • Home
  • Java Tutorial
  • Node.js Tutorial
  • C Tutorial
  • Privacy Policy
  • Contact Us
  • About Us
Tech Stack Journal

Tech Stack Journal

  • Java
  • Node.js
  • Docker
  • Algorithms
  • Spring Core

Get Environment Variables and System Properties in Java

September 18, 2020 by Admin Leave a Comment

Contents

  • 1 What is an Environment Variable?
  • 2 Java Program to Read an Environment Variable
  • 3 What is a System Property?
  • 4 Java Program to Read a System Property

What is an Environment Variable?

An environment variable is a system variable, and any program can access it for various reasons, like locating the path of the program, user names etc., which you would like to make it accessible for all or specific program.

An environment variable often named all in CAPS, for example PATH. Each environment variable contains a value in text format.

Java Program to Read an Environment Variable

We can access Environment variables from within a Java program by using System.getenv. System is a class that is available within java.lang package. So, you don’t have to import this class. Method System.getenv accepts a String value which represents the environment variable and returns a String that contains value of that environment variable.

package com.techstackjournal;

public class ReadEnvVariables {

	public static void main(String[] args) {

		System.out.println(System.getenv("PATH"));

	}

}

What is a System Property?

System property is key and value pair passed as VM argument to the java program. While passing a system property we prefix -D to the system property key-value pair.

Java Program to Read a System Property

To read a system property, we can use System.getProperty method. We need to pass the property key name into System.getProperty method to get the value of that key.

package com.techstackjournal;

public class ReadSystemProperty {

	public static void main(String[] args) {
		
		String companyName = System.getProperty("company_name");

		System.out.println(companyName);

	}

}

If you are running it on Eclipse,

  • right click and select “Run As” => “Run Configuration”.
  • Create new launch configuration
  • Under Arguments=>VM arguments, enter -Dcompany_name=Tech Stack Journal
  • Click on Run

To run this program from command-line:

java -D"company_name=Tech Stack Journal" com.techstackjournal.ReadSystemProperty
Next

Filed Under: Java

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


Primary Sidebar

  • Facebook
  • Pinterest
  • YouTube

More to See

How to Show Last Updated Date in Posts with Genesis Framework

December 12, 2020 By Admin

Get Environment Variables and System Properties in Java

September 18, 2020 By Admin

Tags

access modifiers in java Access Specifiers in Java array in class java ArrayIndexOutOfBoundsException Arrays in Java Constructor Definition in Java Constructor in Java Constructor in Java with Example Definition of Constructor in Java example of a loop Example of Constructor in Java final method final variable First Hello World Java Program for loop in java with example for loop java example for loops in java examples for loops java example How to run Java Hello World from command prompt How to use Constructor in Java How to write Hello World Java program in Eclipse Inheritance in Java Installing Java Installing Java in Windows Introduction to Java Introduction to Java Programming Iterating over an array Java Comments Java Do While Loop Java For Loop Java Hello World Command Line Java Hello World Example Java Hello World in Eclipse Java Hello World Program Java Hello World Tutorial Java Inheritance Java Installation Java Intro Java Introduction Java Statements Java While Loop Static Methods in Java Static Variables in Java Types of Comments What is a Constructor in Java

Secondary Sidebar

Categories

  • Algorithms
  • Blogging
  • Docker
  • Java
  • Node.js
  • Spring Core

Archives

  • January 2021 (1)
  • December 2020 (1)
  • September 2020 (2)
  • August 2020 (5)
  • July 2020 (4)
  • June 2020 (1)
  • May 2020 (4)
  • April 2020 (22)
  • November 2019 (3)
  • September 2019 (2)
  • August 2019 (6)

Footer

Navigation

  • Home
  • Java Tutorial
  • Node.js Tutorial
  • C Tutorial
  • Privacy Policy
  • Contact Us
  • About Us

My Other Sites

Spec Watchers

Speak New Language

Recent

  • [Solved] How to fix java.lang.InstantiationException
  • How to Show Last Updated Date in Posts with Genesis Framework
  • Get Environment Variables and System Properties in Java
  • Autowire using Java Based Spring Configuration
  • Java based Spring Configuration

Search

Tags

access modifiers in java Access Specifiers in Java array in class java ArrayIndexOutOfBoundsException Arrays in Java Constructor Definition in Java Constructor in Java Constructor in Java with Example Definition of Constructor in Java example of a loop Example of Constructor in Java final method final variable First Hello World Java Program for loop in java with example for loop java example for loops in java examples for loops java example How to run Java Hello World from command prompt How to use Constructor in Java How to write Hello World Java program in Eclipse Inheritance in Java Installing Java Installing Java in Windows Introduction to Java Introduction to Java Programming Iterating over an array Java Comments Java Do While Loop Java For Loop Java Hello World Command Line Java Hello World Example Java Hello World in Eclipse Java Hello World Program Java Hello World Tutorial Java Inheritance Java Installation Java Intro Java Introduction Java Statements Java While Loop Static Methods in Java Static Variables in Java Types of Comments What is a Constructor in Java

Copyright © 2021 · Tech Stack Journal · Log in