Languages
[Edit]
EN

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

11 points
Created by:
Suzannah-Estrada
620

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
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join