views:

3183

answers:

12

I have an ASP .NET website that hosts a Java applet. The Java applet requires version 1.6 Update 11 of the Java runtime.

How can I detect that a client has the appropriate runtime installed so that I can display an informative message if they do not?

Thanks,

Carl.

EDIT: The solution must be platform independant.

+5  A: 

This page describes how to and lists some plugins that will allow you to detect Java with JavaScript: http://www.pinlady.net/PluginDetect/JavaDetect.htm

Other than that, try out this snippet as well:

if (navigator.javaEnabled()) {
    //Java is enabled
}
Andreas Grech
Thanks for your response, Dreas. +1. The "JavaDetect" link was very useful and that's what I've implemented. I've tested this in both IE7 and FF 3.05, it works nicely and is easy to implement.
Carl
Glad to be of help mate ;-)
Andreas Grech
Carl: You should probably accept this as the answer, then.
R. Bemrose
And it seems to be platform independent since it also worked in FF in Linux
jassuncao
@R. Bemrose, I probably will accept this answer, but, I want to get value for my Bounty. 8)
Carl
A: 

Maybe this script will help. This is Windows-only, though.

<script language='javascript' type='text/javascript'>
var javaVersion;
var shell;
try 
{
    // Create WSH(WindowsScriptHost) shell, available on Windows only
    shell = new ActiveXObject("WScript.Shell");

    if (shell != null) 
    {
     // Read JRE version from Window Registry
     try 
     {
      javaVersion = shell.regRead("HKEY_LOCAL_MACHINE\\Software\\JavaSoft\\Java Runtime Environment\\");
     } 
     catch(e) 
     {
      // handle exceptions raised by 'shell.regRead(...)' here
      // so that the outer try-catch block would receive only
      // exceptions raised by 'shell = new ActiveXObject(...)'
      alert('error reading registry');
     }
    } 
} 
catch(e) 
{
    // Creating ActiveX controls thru script is disabled 
    // in InternetExplorer security options 

    // To enable it: 
    // a. Go to the 'Tools --> Internet Options' menu
    // b. Select the 'Security' tab
    // c. Select zone (Internet/Intranet)
    // d. Click the 'Custom Level..' button which will display the
    // 'Security Settings' window.
    // e. Enable the option 'Initialize and script ActiveX controls
    // not marked as safe'

    activeXDisabled = true;
} 

// Check whether we got required (1.6) Java Plugin
if ( javaVersion != null && javaVersion.indexOf("1.6")) 
{
    pluginDetected = true;
    alert('it is installed!')
}

if (pluginDetected) 
{
    // show applet page
} 
else if (confirm("Java Plugin 1.6 not found")) 
{
    // show install page
    alert('not found')
} 
else 
{
    // show error page
    alet('error')
}
</script>
SquidScareMe
Thanks for the response SquidScareMe, but I need a platform independant solution. I'll edit the question to stipulate this.
Carl
I figured so much. Sorry I couldn't help. Good luck, though!
SquidScareMe
If you have an environment that allows creation of WScript.Shell objects, you probably have enough control of it to force a roll-out of java
erikkallen
A: 

Copy and paste into your browser address bar to check:

javascript:alert(navigator.javaEnabled())

Works in Mozilla (FF3) and IE7. (x64)

Ed Blackburn
That doesn't give me the runtime version though?
Carl
No it doesn't. the .NET CLR is available through the user agent, but JVM details aren't. I guess it comes down to your environment, if you control it, enforce roll out of Java. If not you may have to take the hit on the user having to be redirected to java.sun.com for an update :o
Ed Blackburn
+5  A: 

My approach would be to use the JavaScript navigator.javaEnabled() to check if there is some Java version available.

Then you can use System.getProperty("java.version") from within a Java applet itself. That should be enough to get you the version information, such as 1.6.0_03.

paxdiablo
This sounds interesting. I'll look into it. +1
Carl
It works, I use it to test JVM's. +1
WolfmanDragon
A: 

If the solution must be platform independent. I'll exclude all solution provided by javascript.

You can write a very simple Applet which support by the oldish Classic VM. Let the applet detect the JRE version, and display messsage.

If you accept Javascript, you may also interest to this article which is about auto-installer for WebStart applications.

Dennis Cheung
Javascript is platform independent. Although not browser independent, any browser supporting applets will most likely support javascript as well...
truppo
+5  A: 

The link below details on the deployment tips for java apps.

http://java.sun.com/javase/6/docs/technotes/guides/jweb/deployment_advice.html

Quoting from the link

Deployment Toolkit To avoid browser compatibility issues, the Deployment Toolkit (deployJava.js) provides JavaScript functions that automatically generate HTML required to deploy applets and Java Web Start applications. Developers should invoke these functions to deploy their solutions in a consistent fashion across various browsers.

Ramesh
+1  A: 

If you don't mind using a basic java applet that requires a much older version to run, you could use this article and continue based on that result.

achinda99
This is a useful resource, thanks. +1.
Carl
A: 
  1. Use deployJava.js function getJREs() to build a dynamic page depending on the user's Java version.

  2. Use http://cowwoc.blogspot.com/2008/12/tracking-java-versions-using-google.html to track the Java version being used by your web visitors using Google Analytics.

  3. Bonus step: use the source-code from step 2 as an example of how step 1 should be implemented.

Gili
A: 

Just copy amd paste this code in your browser address bar! Check if it is useful for you.

javascript: for(i = 0; i < navigator.plugins.length; i++) alert(navigator.plugins[i].name);

You need to obtain the position of Java in the plugins array and maybe you can check the versions with a regular expression or something. You could take a look to the plugin javascript object.

Regards!

Matias
So he'd need to find a way to paste something into the client's address bar from server-side code?
erikkallen
@erikkallen: Since it is javascript just make a function of it and call it in your code. Won't work in IE, but works in FF, Chrome and Opera.
some
+1  A: 

Although most of the answers so far are all around detection on the user agent (browser) itself, it sounds like from your mention of ASP.NET that you'd like to make this detection happen on the server-side. If so, you have a couple of options.

First you can sniff the HTTP request headers coming from the user agent. A computer and browser with Java installed will usually include a header providing a hint of this to the server that you can pick up on. Here are some useful links on this approach: http://www.developershome.com/wap/detection/ http://search.cpan.org/~gwyn/HTTP-Browscap-0.03/Browscap.pm

The other option you have is to send down javascript to the user agent to perform the detection using one of the techniques in the other answers in this question and then use ajax callbacks to tell the server what you discovered.

Andrew Arnott
+1 for the alternative approach and the links Andrew. Thanks.
Carl
A: 

The link below was the way I got it to work. It has a JS you can get which was made for Java deployment. The link was mentioned above, but I thought an example with it would be nice.

These are samples that I ended up needing to fix my problem.

http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/technotes/guides/jweb/deployment_advice.html#deplToolkit

Once you have downloaded the deployJava.js file you could put something to make sure java is installed: if (deployJava.getJREs().length > 0) { //Your code to start java }

Make sure a specific version is installed: if (deployJava.versionCheck(version)) { //Your version specific code }

Start the installer of the latest Java: deployJava.installLatestJava();

Or a specific version if you would like: deployJava.installJRE(version);

CastroXXL