Languages
[Edit]
EN

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

11 points
Created by:
Kate_C
19790

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
Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join