Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

Manage Browser's Java from JavaScript

4.91/5 (6 votes)
12 Nov 2013CPOL3 min read 10.1K  
How to get information and use of Java from JavaScript...

Introduction

In the last few weeks, I bumped into few (at least 3) questions about reading registry from JavaScript code. After seeing them, I realized that all that codes tried to decide what version of JRE (Java Runtime Environment) was installed on the clients machine. This approach can't work for two reasons:

  • No registry, but on Windows OS!!!
  • Registry reading is blocked by browser security (and for good reason)

Fortunately, Java has a deployment toolkit script, that provides several functions to check around about installed Java.

I will now show some of the functions inside that toolkit...

Use the Toolkit Script

To utilize the toolkit, you have to include the JavaScript in your page's header:

HTML
<!DOCTYPE html>
<html>
    <head>
        <title>Java with JavaScript</title>
        <script src="http://java.com/js/deployJava.js"></script>
    </head>
    <body>
    </body>
</html>

The single most important feature is the ability to check if the client has a minimum version of Java installed:

HTML
<!DOCTYPE html>
<html>
    <head>
        <title>Java with JavaScript</title>
        <script src="http://java.com/js/deployJava.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            var is1dot6orbetter = deployJava.versionCheck('1.6+');
        </script>
    </body>
</html>

The version string has a format of,

major[.minor[.rev[_update]]][+|*]

where + means 'this version or better', and * means 'version start like this'.

For instance '1.6+' includes 1.6, 1.6.1 and 1.7 too, but '1.6*' includes only 1.6 and 1.6.1.

Another nice thing is the feature to check if browser has a Java plugin installed. To run a Java applet inside the browser, it's even more important than the version check (version check works on the OS level, as it's possible that the client has JRE installed, but has no plugin in the browser).

HTML
<!DOCTYPE html>
<html>
    <head>
        <title>Java with JavaScript</title>
        <script src="http://java.com/js/deployJava.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            var isPluginInstalled = deployJava.isPluginInstalled();
        </script>
    </body>
</html>

As it's possible to have more than one JRE version installed on the same OS, the toolkit presents an option to get all the installed options. The function below will return a string array where every item holds a version string in the format explained above.

HTML
<!DOCTYPE html>
<html>
    <head>
        <title>Java with JavaScript</title>
        <script src="http://java.com/js/deployJava.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            var allVersions = deployJava.getJREs();
        </script>
    </body>
</html>

Java also has some kind of extension call Java Web Start. This addition enables to download and run Java application from the web, through the browser. If you want to use this feature, it should be wise to check for it before then.

HTML
<!DOCTYPE html>
<html>
    <head>
        <title>Java with JavaScript</title>
        <script src="http://java.com/js/deployJava.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            var isWebStartInstalled = deployJava.isWebStartInstalled();
        </script>
    </body>
</html>

It's rare but entirely possible that there is no Java installed. For that case, the toolkit provides a function to initialize the installation of the latest JRE version.

HTML
<!DOCTYPE html>
<html>
    <head>
        <title>Java with JavaScript</title>
        <script src="http://java.com/js/deployJava.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            deployJava.installLatestJRE();
        </script>
    </body>
</html>

This single line will initiate the JRE install process. The installation will be different of browsers that already have the Java plugin installed from those that have no plugin. In browsers with plugin, the toolkit will use the plugin update mechanism to install the latest version. In those without plugin, the function will redirect the client to the right installation page of JRE.

Summary

If your web application is using Java for some functionality, it perfectly makes sense to check the state of the installation, and provide the user with some nicely presented information and options to fix problems if any. By using this toolkit, you can ensure a single, simple cross platform deployment of JRE. You have to remember that this toolkit was provided by the very same team brought to you by Java itself. It's handling all the browsers and OSes for you, so you can put one problem aside. It also has support, backed by the very same team you call for Java support.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)