【问题标题】:How do I prompt the user to install JRE if she hasn't it already?如果用户还没有安装 JRE,我该如何提示她?
【发布时间】:2009-05-04 14:50:54
【问题描述】:

我正在编写一个 Java 小程序,并且想知道将它包含在网页中的最佳方式。

如果她还没有安装 JRE,我希望它提示用户安装 JRE。 此功能应该(理想情况下)在运行 Java 的任何操作系统上跨浏览器工作。 另一个要求是不应在页面加载时加载小程序,而是在用户操作之后加载,而不是在每次加载页面时加载 JVM。 我猜this is the official SUN way,但是它使用document.write(),所以页面渲染完成后我不能使用它。

【问题讨论】:

  • 这不是浏览器的功能吗?诸如“您缺少查看此页面所需的插件”之类的内容?

标签: java deployment applet


【解决方案1】:

我建议只使用小程序标签。正如 Alex B cmets 一样,如果用户没有 JRE,大多数浏览器会在此时提示用户安装。

即使 Sun 也推荐 using the applet tag except on Intranets,除非您在 Intranet 上。我想这里的逻辑是您可以在内部服务器上托管 JRE 下载,并使用 embed & object 标签将下载定向到该服务器。

我曾经使用 embed & object 标签,但由于版本号,这最终很麻烦。假设您需要 Java 1.5。因此,您在对象和嵌入标签中指定,以确保用户在没有 1.5 时必须升级。但是,这并不是您真正想要的,您希望它将它们升级到最新的 JVM。无论如何,这不是我上次玩它时最聪明的行为——现在他们可能已经改进了它。

【讨论】:

    【解决方案2】:

    我同意 jwls,最好使用 applet 标签,因为使用 embed 和 object 很难获得正确的跨浏览器 - 以至于自定义每个浏览器都需要设置。

    但是,使用 applet 标签时,您需要注意 Microsoft 的 VM 1.1 上的用户。当我在二月份进行测试时,它们仍然占5% of Java versions。如果这些用户访问需要更新版本的页面,他们将看到一个可怕的灰色区域。

    解决方案(在 java.net 上讨论后)是使用一个小程序来检查 Java 版本并在不满足目标版本时重定向到失败页面。这是我的来源:

    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;
            }
        }
    }
    

    示例 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-03
      • 2018-05-10
      • 2011-12-16
      • 1970-01-01
      • 2013-05-06
      • 2023-03-04
      • 1970-01-01
      相关资源
      最近更新 更多