w3hJava

What, Why, When and How of Java, JavaFX and related technologies


Published June 17th, 2008

Test your applet on different JRE’s

In last post on StringBuffer, Dimitris Andreou commented that it was an old thing. ;-). So, I decided to write something new which is currently going on in Sun Microsystems. Finally I zeroed down on Java SE 6u10 features. Some of them I blogged on my Sun site but here are some more.

Very often this happens. You made a UI in applet and want to check whether the application is working fine on all JRE’s or not. If you are not, then please do it :).?I have seen people still using JDK 1.3. I, myself use jdk1.4.2 for many reasons and it still survive somewhere in the company standards. Now, if you want to check the applet on all JRE’s, welcome to the world of JRE 6u10 features.

Here is the demo:

Write a normal applet code, say Hello World.

import java.awt.*;
import java.applet.Applet;

public class HelloWorld extends Applet {
public void paint(Graphics g) {
g.drawString("Hello World",50,50);
} // end of paint
} // end of applet

And now the interesting part, write the HTML file like:

<APPLET CODE="HelloWorld.class" WIDTH=200 HEIGHT=100>
<PARAM name="java_version" value="1.5.0_20">
</APPLET>

It will run the applet on JRE 1.5.0_20(if exists on machine). For more generic once, write:

<APPLET CODE="HelloWorld.class" WIDTH=200 HEIGHT=100>
<PARAM name="java_version" value="1.4*">
</APPLET>

It will check any flavor of 1.4 like 1.4.2_01 or 1.4.2_10 on your machine. 6u10 rocks ! Remember it will work in IE6 or + and FF3 or +. Ah, I remember, today is FF3 download day. So, please don’t forget to download FF3.

Published May 11th, 2008

Number of JREs on my Windows machine?

Sometime back, we were discussing something about JRE and one of my friends Lawrence asked me “How to find the number of JRE  installed on system with the help of Java Code ?

After giving much thought to it, I don’t think Java has any such API which will tell how many JRE are installed on the System and what are they ? One of my another friend Vikram told me that JRE installation records information in Registry. With this knowledge at the hindsight, I tried to write this piece of code.

It will only run on Windows :) because I have used Runtime class. I would love to know how the same could be achieved in Unix Systems. This code is not doing anything great, just queries the registry and reflects the answer on the console.

import java.io.*;
class NoofJRE {
    static String REG_PATH = "reg query " +
                          "\"HKLM\Software\JavaSoft\Java Runtime Environment";
       public static void  getJREInfo() {
            try {
            Process process = Runtime.getRuntime().exec(REG_PATH);
                  InputStream inputstream = process.getInputStream();
                  InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
                  BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
                  String line;
                  while ((line = bufferedreader.readLine()) != null) {
                           System.out.println(line);
                  }
          }
            catch (Exception e) {
                  System.out.println("I am in Exception");
        }
      }
      public static void main(String s[]) {
            getJREInfo();
        }
}

And here is my output:

E:\Program Files\Java\jdk1.6.0\bin>java NoofJRE
! REG.EXE VERSION 3.0
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment
CurrentVersion REG_SZ 1.6
BrowserJavaVersion REG_SZ 1.6.0_01
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment\1.4.2_17
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment\1.6
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment\1.6.0
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment\1.6.0_01

Which sounds true in my case. It is clearly telling me that I have JRE 1.4.2_17, 1.6.0 and 1.6.0_01. I have old bad habit of not uninstalling JRE’s :).

Please do let me know if there is any other way to know how many and which JRE is/are installed in my system.

To know where the JRE is installed, one has to do a simple query of JAVA_HOME in the registry.