Home > Java, Javascript > Java Plugin detection using JavaScript

Java Plugin detection using JavaScript

In this article we will understand how to detect whether the client browser has Java Plugin Installed. This process involves communication between Java and JavaScript.

NOTE:
This tutorial cannot detect the Java Plugin Version installed.
The following steps will guide you to process of detecting Java Plugin on client machine using JavaScript:

  • 1. Create Java Applet
    Java Applets are used for handshaking purpose as these can be directly embedded inside HTML document using Applet tag. JavaScript will then refer to the applet using the Applet Name, the Applet contains an public function which can be called directly from JavaScript.
1
2
3
4
5
6
7
8
9
import java.applet.Applet;
import java.awt.*;
public class checkJava extends Applet 
{
    public boolean setMessage() 
	{
		return true;
    }
}

Explanation for Java Code:

  • Line 1 – Line 5 defines the basic code required for creating an Java Applet
  • On Line 6, declared an public function setMessage() that will return an boolean value true
  • 2. Writing JavaScript Code:
    Next step would be to write an JavaScript code that does the communication with the Java Applet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<html>
    <body>
        <applet code="checkJava"
            width="1"
            height="1"
            mayscript="mayscript"
            name="checkJava">
        </applet>
 
        <script language="javascript">
		try
		{
			var x = checkJava.setMessage();	
			alert(x);
		}
		catch(e)
		{
			alert("Java Plugin Not Installed");
		}	
        </script>
    </body>
</html>

Explanation for JavaScript Code:

  • Here i have placed the java applet inside body tag using applet tag providing with the necessary parameter. Please make sure that mayscript is there inside the applet tag. Without mayscript attribute the handshake will not work
  • Applet can be referred inside javascript by the applet name. Referring to Line no 14, calling the setMessage() function by using the Applet Name
  • Variable x will hold the value “true” if there was successful communication between JavaScript ans Java Applet
  • The try…catch is required to catch the error in case the JavaScript is unable to communicate with Java Applet
  • Finally in the catch block i am giving an alert message


Custom Search

Popular Articles:

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • IndianPad
  • LinkedIn
  • Live
  • MySpace
  • Netvibes
  • RSS
  • Technorati
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • Reddit
  • Add to favorites
  • PDF
  • Twitter
Categories: Java, Javascript Tags: ,
  1. kopsik
    December 9th, 2007 at 02:55 | #1

    It may take a while, until the applet loads, but the script is executed immediately. So I don’t believe this is a reliable solution. Sorry.

  2. December 9th, 2007 at 08:23 | #2

    Hi kopsik,
    Nice to hear this feedback from you. But i searched the internet for an good solution on this but unfortunately JavaScript alone cannot detect Java Plugin if you are targeting Multiple Browsers i.e. IE, Mozilla Firefox, Opera etc. Also i checked this solution on Firefox, IE, Opera and it works fine.

  3. April 4th, 2008 at 05:39 | #3

    Yes, it is possible, but there might be an delay.

    See http://www.browserreport.com/ for crossbrowser Java detection.

  4. Sachin Lokare
    June 28th, 2008 at 02:05 | #4

    Hi Hitesh,

    checkJava.setMessage();
    is not working in Mozilla 3 version
    it says that checkJava.setMessage() is not a function

    I have used like this

  5. June 30th, 2008 at 03:24 | #5

    Hi Sachin,
    The javascript engine parser differs for browsers. Will have to check how we can detect the same on Firefox 3.0.

    Hitesh Agarwal

  1. No trackbacks yet.