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