Find out how to extract cool information about the environment in which your Java code is running One query that I see fairly often is: “How can I find out the name of the browser that my applet is running in?” This question usually stems from the need to work around various differences in the various JREs that are included in the different browsers. It turns out that the code to do this is really quite simple.A quick note about terminology: In Java-speak what other people call environment variables we call properties. Java properties are a bit more flexible than the environment variables available in other systems (see the Resources for more information).Here’s a simple bit of code to extract all of the properties currently in the system: Properties props = System.getProperties(); Enumeration e = props.propertyNames(); while (e.hasMoreElements()) { key = (String) e.nextElement(); value = props.getProperty (key); textArea.appendText ("Key :" + key + ": = :" + value + ":n"); } The Properties class is basically just a hashtable of pairs of environment variables and their associated values. We just ask the props object for an enumeration of all of its property names and iterate through that list, requesting the appropriate, associated value.Unfortunately, if you run the above code in any “secure” environment — in an applet, for example — you are quite likely to encounter a SecurityException. This little annoyance occurs because security folks think (rightfully so) that it’s dangerous to show all of the environment variables, um, I mean properties, because this allows access to confidential information about the end user. So, as a precaution they decided to have the security manager throw an exception in a secure environment.Of course, a better approach would have been to change System.getProperties() to recognize that it’s running in a secure environment and return only those properties the security folks think applets should have access to. But no! Not to fear. All is not completely lost. For applets, we simply ask for each of the individual properties separately rather than requesting the entire properties list at one time.Here’s code that will extract the standard, “safe” information: String keys [] = { "java.vendor", "java.vendor.url", "java.version", "java.class.version", "os.name", "os.arch", "os.version", "file.separator", "path.separator", "line.separator", "browser", "browser.vendor", "browser.version" }; for (int i = 0; i < keys.length; i++) { try { key = keys[i]; value = System.getProperty (key); textArea.appendText ("Key :" + key + ": = :" + value + ":n"); } catch (SecurityException see) { ; } } This code is similar to what we saw earlier, but instead of trying to get all of the properties from the system, we just hard code the list of known safe properties in an array and iterate through the array. Here’s the example applet in action:The results of the Java applet should be here.The applet code, PropertyLister.java (see Resources), combines both approaches; it first tries to retrieve all properties and if that fails, tries the individual approach. I’ll leave it as an exercise for you to put that code into a utility method that returns a Properties object regardless of the security situation, allowing the rest of your code to ignore the situation (or not) as it chooses.Well, there you go. You can now safely extract all of the standard, safe properties regardless of the context that your programs are running in. Alternately as employee, consultant, and principal of his own company, John D. Mitchell has invested the last ten years in developing cutting-edge computer software and advising other developers. John co-authored Making Sense of Java: A Guide for Managers and the Rest of Us and has published articles in programming journals. In addition to writing the Java Tips column for JavaWorld, he moderates the comp.lang.tcl.announce and comp.binaries.geos newsgroups. JavaTechnology Industry