Friday, May 6, 2011

How do I prompt the user to install JRE if she hasn't it already?

I am writing a Java applet, and would like to know the best way to include it in a web page.

I'd like it to prompt the user to install JRE if she hasn't it already. This feature should (ideally) work cross-browser on any OS Java runs on. Another requirement is that the applet should not be loaded on page load, but after a user action, not to load the JVM on every page load. I guess this is the official SUN way, but it uses document.write(), so I can't use it after the page has finished rendering.

From stackoverflow
  • I would recommend just using the applet tag. As Alex B comments, most browsers will prompt the user to install at that point if they don't have the JRE.

    Even Sun recommends using the applet tag except on Intranets unless you are on an intranet. I imagine the logic here is that you can host the JRE download on an internal server, and use the embed & object tags to direct the download to that server.

    I used to use the embed & object tags, but that ends up being a hassle due to the version numbers. Lets say you require Java 1.5. So you specify that in the object & embed tags to make sure the user will have to upgrade if they don't have 1.5. However, that's not really what you want, you want it to upgrade them to the very latest JVM. In any event, it was not the smartest behavior when I last played with it-- it's possible they have improved it now.

  • I agree with jwls, it is better to use an applet tag because using embed and object are very awkward to get right cross browser - to the point where a custom setup per browser is getting necessary.

    However using the applet tag you need to beware of users on Microsoft's VM 1.1. When I tested in February they still accounted for 5% of Java versions. If these users visit a page where a later version is required they will see a horrible grey area.

    The solution for this (after discussion on java.net) was to use a small applet which checks the Java version and redirects to a failure page if the target version isn't met. Here is my source:

    JavaRedirectorApplet.java

    import java.applet.Applet;
    import java.net.URL;
    
    /**
     * Applet built for bytecode 1.1
     * 
     * If applet is less than a set level redirects to a given page, else does nothing
     */
    public class JavaRedirectorApplet extends Applet {
    
        /** The required java version */
        private final static String PARAM_REQUIRED_JAVA_VERSION = "REQUIRED_JAVA_VERSION";
    
        /** The failure page */
        private final static String PARAM_FAILURE_PAGE = "FAILURE_PAGE";
    
        /**
         * Initializes the applet
         */
        public void init() {
    
            // evaluate the required Java version
            double requiredJavaVersion = -1;
            String requiredJavaVersionString = getParameter(PARAM_REQUIRED_JAVA_VERSION);
            if (requiredJavaVersionString != null) {
                try {
                    requiredJavaVersion = Double.valueOf(requiredJavaVersionString).doubleValue();
                } catch (Exception e) {
                    // ignored, caught below
                }
            }
    
            if (requiredJavaVersion < 0) {
                System.err.println(PARAM_REQUIRED_JAVA_VERSION + " not set or set incorrectly (must be set to a number greater than 0)");
                return;
            }
    
            // get the failure page
            URL failurePageURL = null;
            String failurePageString = getParameter(PARAM_FAILURE_PAGE);
            if (failurePageString != null) {
                try {
                    failurePageURL = new URL(getCodeBase().getProtocol(),
                                        getCodeBase().getHost(),
                                        getCodeBase().getPort(),
                                        failurePageString);
                } catch (Exception e) {
                    // ignored, caught below
                }
            }
    
            if (failurePageURL == null) {
                System.err.println(PARAM_FAILURE_PAGE + " not set or set incorrectly (must be set to a valid path)");
                return;
            }
    
            // check to see whether valid
            if (!isValidVersion(requiredJavaVersion)) {
    
                // not valid redirect self
                getAppletContext().showDocument(failurePageURL, "_self");
            }
    
            // seems fine
        }
    
        /**
         * Check the Java version against a required version
         *
         * @param versionRequired
         * @return the verdict
         */
        public static boolean isValidVersion(double versionRequired) {
            try {
                double javaVersion = Double.valueOf(System.getProperty("java.version").substring(0, 3)).doubleValue();
    
                if (javaVersion < versionRequired) {
                    return false;
                } else {
                    return true;
                }
            } catch (NumberFormatException e) {
                return false;
            }
        }
    }
    

    Example HTML

    <!-- place before the actual applet -->
    <div style="display: none;">
        <applet code="JavaRedirectorApplet" width="0" height="0">
            <param name="REQUIRED_JAVA_VERSION" value="1.4"/>
            <param name="FAILURE_PAGE" value="/failurePage.html" />
        </applet>
    </div>
                                                

0 comments:

Post a Comment