Languages
[Edit]
EN

Java - how to get single system environment variable value by name?

11 points
Created by:
Kate_C
2854

Java - get single system environment variable value by name - example

This example prints most commonly used system environment variables in java by their name.

import java.util.Map;

public class JavaGetSingleEnvironmentVariablesExample {

    public static void main(String[] args) {
        Map<String, String> env = System.getenv();

        String os = env.get("OS");
        System.out.println(os); // Windows_NT

        String username = env.get("USERNAME");
        System.out.println(username); // seth

        String numberOfProcessors = env.get("NUMBER_OF_PROCESSORS");
        System.out.println(numberOfProcessors); // 4

        String path = env.get("Path");
        // C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Wind ....
        System.out.println(path);

        String windir = env.get("windir");
        System.out.println(windir); // C:\Windows

        String processorArchitecture = env.get("PROCESSOR_ARCHITECTURE");
        System.out.println(processorArchitecture); // AMD64

        String systemDrive = env.get("SystemDrive");
        System.out.println(systemDrive); // C:
    }
}

Output:

Windows_NT
seth
4
C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Wind ....
C:\Windows
AMD64
C:

References

  1. System.getenv() - Java Docs
  2. Environment variable - wiki

Checkout latest findings & news:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Rockstar community members
Thank you for great work 👍