Listing the currently defined environment variables and system properties in Java
On the previous page, we introduced the concept of
environment variables and system properties.
Conceptually, the two are very similar: the essential difference is that environment
variables are defined and managed by the operating system, whereas system properties
effectively "belong" to Java and in some cases are "translations" of environment
variables in order to bring more consistency across platforms.
We also listed some commonly defined environment variables and system properties
on the previous page. However, it is sometimes useful to interrogate the full list
of variables/properties defined on a given system from within Java. We will look at
how to accomplish that task here.
How to refer to all variables/properties in memory: map data structure
First of all, we need a data structure in which to store all variables and properties.
An environment variable or system property is essentially a mapping or
association of one String (the variable/property name) to another String.
So, we will assume that the type of object used is some
form of Map of String to String, such as a HashMap. If you're not
familiar with the concept of a map or hash map, then I recommend that you take
a brief detour to the Java collections
section of this web site, and in particular to the section on
Java maps and the
HashMap class.
So, given some object implementing Map, we can write a method to dump out
the "list" (list of mappings) of variables or properties as follows. As a bonus,
we'll order the variables/properties.
private static void dumpVars(Map<String, ?> m) {
List<String> keys = new ArrayList<String>(m.keySet());
Collections.sort(keys);
for (String k : keys) {
System.out.println(k + " : " + m.get(k));
}
}
We actually allow specify the map as being a mapping of Strings to any
object type, as this is sufficient for our purposes: the toString() method,
which is what will actually get called on each value when printing it out, is
defined on Object and is not specific to String. On the other
hand, we requrie the keys to be comparable for sorting, and since in reality we are
expecting Strings, it makes sense to specify String as the key class.
Listing environment variables vs properties
Now, conceptually we've said that environment variables and system properties
are essentially variations on a theme, and assumed in our dumpVars() method
that in either case, we have a map of strings to variables. It turns out that:
- environment variables are enumerated using the method
System.getenv(), and that method does indeed return a Map
of Strings to Strings;
- system properties are enumerated with the method System.getProperties
which, for historical reasons, returns a Properties object.
For dumping environment variables, we can therefore pass the output of
System.getenv() directly into our method:
System.out.println("===== ENV VARIABLES =====");
dumpVars(System.getenv());
In the case of the Properties object output by System.getProperties(),
this is not strictly speaking a map of String to String (or other object), but actually
a map (the older Hashtable class) of Object to Object. A quick-and-dirty
solution is to pass the Properties object into the constructor of a HashMap:
System.out.println("===== PROPERTIES =====");
dumpVars(new HashMap(System.getProperties()));
Technically, this creates a "raw" (i.e. not generically typed)
HashMap object. The compiler will then—albeit with a warning—
allow us to pass this raw HashMap
into our dump method, even though strictly speaking that method is defined as taking
a typed HashMap, and will "trust" that the objects inside the map
can be cast to the correct type at runtime. This behaviour is really supported for backwards
compatibility but it avoids us some long-winded code.
If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.
Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.