Cross-platform/browser method to detect java plugin

I was looking for a reliable way to detect if the java plugin is available on a browser, i’ve had a look at various javascript methods and sun’s deployJava.js but they all seem a little unreliable especially on browser like safari and konqueror where no information is reported at all from javascript.

I’ve since found the only reliable way is to actually have a java applet launch and report back that it is available.

Thought i’d post the method here in case if it is useful to others.

java code

import java.applet.Applet;
import netscape.javascript.JSObject;

public class Test extends Applet {
	
	JSObject win;
	
	public void init() {
		
		win = JSObject.getWindow(this);
		
		Object[] param = { System.getProperty("java.version") };
		
		// alert javascript that java plugin is found
		win.call("javaSupported", param);
	}

}

html/javascript code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<body onLoad="checkJava()">


<script type="text/javascript">

    var javaFound = false;

    function javaSupported(version) {
      javaFound = true;
      var gamebox=document.getElementById('appletbox');
      // remove applet
	  gamebox.innerHTML = 'Java version ' + version + ' available';
    }

    function javaNotFound() {
		if (!javaFound) {
            var appletbox=document.getElementById('appletbox');
            appletbox.innerHTML = 'Java Not Found';
        }
    }

    function checkJava() {
        // wait 10 seconds if java hasn't responded presume its not found
        setTimeout('javaNotFound();', 10000);
    }
</script>

<div id="appletbox">Checking if Java Available...<applet code="Test" width="1" height="1" MAYSCRIPT/></div>

</body>
</html> 

What the code does is wait 10 seconds for the applet to report back through liveconnect that it is available, if it does not report back it presumes that java is not available.

Another thing that makes this faster than using a full applet is that the innerHTML quickly removes the applet once it has reported back and avoids any further slow downs from the applet that the paint methods, etc bring. You can just change this if you are using a full applet to leave applet there instead of removing it.

example http://kappa.javaunlimited.net/temp/testjava.html

works for me on all the browsers and platforms i’ve tested on IE6,IE7,Opera,Safari,Firefox,Chrome, Konqueror on windows and linux.

It works, nice job.

I tried it on IE(wich ahs java)

and chrome wich doesnt sadly:(

and both gave the correct answer back.

good job

New applets grab the focus.

If you’re filling a HTML form, and the applet is launches, all key events will go to the applet.

This is very confusing for the user.

It will also freeze the browser for a couple of seconds, in versions under u10.

And there are 101 liveconnect bugs.

Including, IME.

It more than just freezes… it consistently, completely crashes firefox for me. I’m not sure what the exact cause is.

That’s with the 1.6.0_10-b33 plugin, running on OpenSolaris 2008.11

http://people.fh-landshut.de/~mbien/cafebabe.html

one method uses the deployment toolkit the other via mime types. It never happened to me that both didn’t work (but I haven’t tested konqueror). The JavaFX deployment toolkit recently got the mimetype querie too as fallback.

both methods there fail for me in opera.

Yup, also fails for me in Opera.

Then I again, I never managed to get LiveConnect to work in Opera, but given the marketshare of Opera, it’s not top priority for me.

Opera doesn’t recognize the “javascript:” protocol either, which behaves as eval(String) in MSIE and FF.

The two scripts also fail in Safari, it just goes to show the reliabilty of Suns deployment toolkit, besides it rather tedious and difficult to adjust/update the javascript each time a new browser comes out or for every browser thats out there.

However the above applet code works on both safari and opera ;D

True but hopefully it won’t be used on a page that has a html form or if it does the applet will have loaded and closed before the user has a chance to focus the html form. Besides you could probably get focus back using javascript.

Since this is very small class the browser freeze is smaller than that of when using a big jar and since the applet is closed before or when the init() method finishes it futher reduces the delay (thus not reaching the paint methods), however there is still a small delay just not as much as normal applets.

Yes thats true, however this method uses very little liveconnect (only one method) and most of the bugs relate to browser to plugin communication (javascript to java), this is only plugin to browser communication (java to javascript). This should hopefully avoid most of those bugs especially some of those browser specific ones (for example with firefox).

Chrome worked for me.

The browser freeze is so nasty, that my approach used to be to load the applet after X seconds, under the assumption that people where reading the page at that time. But, man… that did backfire.

Unfortunately not. Even a pixel that is 1x1 px, and renders 1 color (for debugging) simply freezes the browser for up to 10 seconds on slower PCs with pre u10 Java versions.

You only need 1 bug, and it stops working. It’s sad.

It’s just that there is NO reliable way to figure out whether Java is supported in the browser, without annoying the end user.

The reason I dont have java is tath I dont have admin users…

so sad.

I code on a school comp. I am gunna have to cmpy a lot of crap off when I give it back in 3 years :). will take a while ot get off all of my crap, my java folder is currently 934 megabytes. that does include jdk and compilers though.

back on topic :wink:

it really takes 10 seconds to load a 1x1 screen. so does a 10x10 take 100 seconds, or is it jsut the fact that is ti s a n applet.

Yes, a 800x600 applet takes 1333 hours to load, never noticed?

i am a js noob, this was actually my first script i’ve written so far. Could you update it in a way that opera could interpret it without breaking it for IE and FF?

i updated the script to use javafx deployment toolkit.

the javafx script work in opera now but still fails in safari.

what do you get as result? “0 - java not enabled” or a empty box?

also could you try this page? it uses a different version of the dtfx.js

I get an empty box and also same result on konqueror.

looking at the code for dtfx.js i’m very impressed with it, it is much more advance and uptodate when compared with the standard deployJava.js as it has support for much more browsers like opera and chrome.

Not sure why sun doesn’t just use this script for both deployment toolkits and kill the older and less compatible deployJava.js

as for using javascript to detect java on safari, I don’t think it can be done with pure javascript since the browser doesn’t support it, the best that can be done is just check if it is safari and if so then just try run the applet whether java is present or not.

they use deployJava.js from within dtfx.js. I guess the only reason to have two scrips is simple because JavaFX development runs asynchronously and more rapidly compared to JRE development. Its easier to introduce api incompatibilities in JavaFX as in Java, a separated deployment script simplifies things.

I see no reasons which prevent you from using dtfx.js for plain java deployment but i would prefer a merged document also.