Java detect brower, JVM vendor, Java version etc

chris (2004-06-14 16:24:05)
14490 views
1 replies
Just about the biggest obstacle to seamlessly deploying java applications to the end user is the JVM. The situation worses when deploying applets due to the patchy java support in some runtime environments. In particular, the Microsoft JVM which ships as standard with Internet Explorer only supports (at time of writing) java version 1.1.x, so anything which uses an api beyond the Java 1.1 api (which is nearly 10 years old) won't work.

To make the situation worse, Microsofts implementation of JavaScript does not allow a full interrogation of the plugin and so will not allow a test of the java version supported. The only way to get around this is to write an applet to set public memebers (for the java version, Operating System (OS) name, OS version etc) and then to interrogate these using LiveConnect. Read on and I will show you how easy it is to run these tests.

The Applet
----------
First you must provide an applet such as the one below. Note, however that this applet is going to run on systems which 'claim' to have java enabled, but because the JVM in question may only support Java 1.1, we have to compile the applet in a particular way. the simple difference is that we must use the 'target' option at compile time to specify the version which we are building for. Here's an example of how you might compile the applet sources below:

$ javac -target 1.1 Tester.java

The result then will be an applet which will run in any environment which supports java 1.1.x. So give it a try - past the following code into your text editor or IDE and compile it for 1.1 and then place the class file alongside the webpage where it will eventually be embedded.

import java.net.*;

/*
 * PluginTest.java
 * Created on 04 June 2004, 17:30
 */

/**
 *
 * @author  Chris Lacy-Hulbert
 * $Id$
 */
public class Tester extends java.applet.Applet {
    public String OSName;
    public String Vendor;
    public String URL;
    public String Version;
    public String OSArch;
    public String OSVersion;

    URL location;
    /** Creates a new instance of PluginTest */
    public void start() {
        Vendor =  System.getProperty("java.vendor");
        URL = System.getProperty("java.vendor.url");
        Version = System.getProperty("java.version");
        OSArch =  System.getProperty("os.arch");
        OSName = System.getProperty("os.name");
        OSVersion = System.getProperty("os.version");
    }
}

But I don't want an applet on the page!
---------------------------------------
Remember, I said that you would only play this applet if the broswer reports that java is enabled. So first you need to test whether or not the system thinks it can play java. This is done with the following snippet of JavaScript:

if (navigator.javaEnabled()){
// run the JVM test
}

The line within the executed block shows us where the applet will be embedded. This is most easily done using the document.writeln() syntax in JavaScript, so the whole lot would look like this:

if (navigator.javaEnabled()){
document.writeln("");
}

And now you can see the sneaky trick. The line of html which is written into the document will pull in the applet, but will give it a width and height of zero. This means that the applet will be as good as invisible. Now all we have to do is write another bit of javascript to plug into the applet and get from it the information we're after.

This is done using a technology called LiveConnect. LiveConnect enables communication between JavaScript and Java applets in a page. In its simplest implementation, Javascript code is written which can directly access public members of java classes. This is done by accessing childnodes of the document.applets DOM members. Sound difficult? It's not. Read on

The JavaScript and LiveConnect
-------------------------------
So the applet is running in the browser and it has set a bunch of public String data members. All we need to do now is access them with a bit of javascript. What you do from there is totally up to you. Here is the full javascript code which will load in the applet, read some values from it and present them in an alert box. If you prefer, you could just test the Java version string and handle accordingly. A simple solution would be to redirect the user to the http://java.com download page.
if (navigator.javaEnabled()){
    document.writeln("");
    if(document.applets[0].Version < 1.4){
         alert("your JVM only supports java version "+document.applets[0].Version);
    }
}

I have presented a very simple set of circumstances and some very simple solutions. You can be really extravagant and get java to write out lines of javascript, or something else equally scary. Whatever you do, have fun and post your comments and replies to this article if you want by clicking on the 'reply' icon below.

christo
comment
mrrena
2008-07-23 20:50:34

Thanks, Chris

Very cool. Just in case there is anybody out there who needs a jump start, I've compiled and uploaded the applet. You can download it at http://silenceisdefeat.com/~mrrena/Tester.class. Save it in the same directory as your JS test and fill in the writeln like so:

document.writeln('<applet code="Tester.class" codebase="." align="baseline" width="0" height="0">no java 2 sdk, standard edition v 1.4.2 support for applet!!</applet>');

Alternately, you can use the online compiler at http://www.innovation.ch/java/java_compile.html.
reply icon