Top community members
All Wiki Articles Create Wiki Article

Have you ever heard about encyclopedia for code?

What about: Wiki for Code project?

* because I've tried to share code and my solutions on different services and it was removed or I was unwelcomed
   - maybe it wasn't good place, but this one is.

if you think you have solution that can help someone, you are welcome to share this solution - Click here

Java - How to print OS / system environment variables?

0 contributions
1 points

In java we can use System.getenv() to get a Map with all current system environment variables.

// fast way to print all environment variables
Map<String, String> env = System.getenv();
env.forEach((key, value) -> System.out.println(key + " : " + value));

1. Print system environment variables - example

import java.util.Map;

public class JavaPrintSystemEnvironmentVariables {

    public static void main(String[] args) {

        Map<String, String> env = System.getenv();

        for (Map.Entry<String, String> entry : env.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();

            System.out.println(key + " : " + value);
        }
    }
}

Output:

PROCESSOR_LEVEL : 6
FPS_BROWSER_APP_PROFILE_STRING : Internet Explorer
LOGONSERVER : \\DESKTOP-MMNVSIU
SESSIONNAME : Console
ALLUSERSPROFILE : C:\ProgramData
PROCESSOR_ARCHITECTURE : AMD64
SystemDrive : C:
USERNAME : seth
OS : Windows_NT
NUMBER_OF_PROCESSORS : 4
windir : C:\Windows

...

This this output of some of the system environment variables.

2. Java 8 - Print system environment variables - example

import java.util.Map;

public class JavaPrintSystemEnvironmentVariablesJava8 {

    public static void main(String[] args) {

        Map<String, String> env = System.getenv();

        env.forEach((key, value) -> System.out.println(key + " : " + value));
    }
}

Output:

PROCESSOR_LEVEL : 6
FPS_BROWSER_APP_PROFILE_STRING : Internet Explorer
LOGONSERVER : \\DESKTOP-MMNVSIU
SESSIONNAME : Console
ALLUSERSPROFILE : C:\ProgramData
PROCESSOR_ARCHITECTURE : AMD64
SystemDrive : C:
USERNAME : seth
OS : Windows_NT
NUMBER_OF_PROCESSORS : 4
windir : C:\Windows

...

This this output of some of the system environment variables.

3. Print system environment variables in sorted order A-Z - example

This example will print all system environment variables in alphabetical order from A to Z.
To keep the order of keys we use TreeSet<String> and String class has default comparator.

import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

public class JavaPrintSystemEnvironmentVariablesSorted {

    public static void main(String[] args) {

        Map<String, String> env = System.getenv();
        SortedSet<String> keys = new TreeSet<>(env.keySet());

        for (String key : keys) {
            String value = env.get(key);

            System.out.println(key + " : " + value);
        }
    }
}

Output:

=:: : ::\
ALLUSERSPROFILE : C:\ProgramData
APPDATA : C:\Users\seth\AppData\Roaming
COMPUTERNAME : DESKTOP-MMNVSIU
ComSpec : C:\Windows\system32\cmd.exe
CommonProgramFiles : C:\Program Files\Common Files
CommonProgramFiles(x86) : C:\Program Files (x86)\Common Files
CommonProgramW6432 : C:\Program Files\Common Files
DriverData : C:\Windows\System32\Drivers\DriverData
FPS_BROWSER_APP_PROFILE_STRING : Internet Explorer
FPS_BROWSER_USER_PROFILE_STRING : Default
HOMEDRIVE : C:
HOMEPATH : \Users\seth
LOCALAPPDATA : C:\Users\seth\AppData\Local
LOGONSERVER : \\DESKTOP-MMNVSIU
MOZ_PLUGIN_PATH : C:\Program Files\Tracker Software\PDF Viewer\Win32\
NUMBER_OF_PROCESSORS : 4
OS : Windows_NT
OneDrive : C:\Users\seth\OneDrive
OneDriveConsumer : C:\Users\seth\OneDrive
PATHEXT : .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE : AMD64
PROCESSOR_IDENTIFIER : Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
PROCESSOR_LEVEL : 6

...

4. Java - get single system environment variables - example

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 systemDrive = env.get("SystemDrive");
        System.out.println(systemDrive); // C:
    }
}

Output:

Windows_NT
seth
4
C:

5. System.getenv() - description

In above examples we used System.getenv().

What this method really do?

Syntax:

public static Map<String,String> getenv()

From java docs:

Returns an unmodifiable string map view of the current system environment.

The environment is a system-dependent mapping from names to values which is passed from parent to child processes.

If the system does not support environment variables, an empty map is returned.

Returns:
the environment as a map of variable names to values

Merged questions

  1. How to print system environment variables from Java application?
  2. How to read system environment variables in java?
  3. Java - how do I get OS environment variables?
  4. How do I get single system environment variables in java?

References

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

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more