Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 53: So what browser is this, anyway?

Find out how to extract cool information about the environment in which your Java code is running

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
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.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • Download the applet code as a zip file /javaworld/javatips/javatip53/jw-javatip53.zip
  • Download the applet code as a gzipped tar file /javaworld/javatips/javatip53/jw-javatip53.tar.gz
  • Find out more about class java.util.Properties http://www.javasoft.com/products/jdk/1.1/docs/api/java.util.Properties.html