处理抛出java.lang.ExceptionInInitializerError的Applet,阻止它运行

时间:2012-02-21 15:19:02

标签: java applet

我们正在开发java applet并将其嵌入到我们的网页中。当通过HTML APPLET标记加载applet时,浏览器/ JVM会提示用户允许它运行。当我们点击取消时,java控制台会指示以下异常:

java.lang.RuntimeException: java.lang.ExceptionInInitializerError
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ExceptionInInitializerError
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.security.AccessControlException: access denied (java.lang.RuntimePermission getenv.TEMP)
    at java.security.AccessControlContext.checkPermission(Unknown Source)
    at java.security.AccessController.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)
    at java.lang.System.getenv(Unknown Source)
    at downLoadApp.<clinit>(downLoadApp.java:15)
    ... 21 more
Exception: java.lang.RuntimeException: java.lang.ExceptionInInitializerError

现在,我意识到这很可能是因为尝试加载的类被阻止加载,因此引发了异常,但我们如何在浏览器中优雅地处理这种情况?我想检测applet是否被拒绝并对该条件发布合理的响应,但我不知道如何捕获此异常,因为它似乎是由于JVM因为没有获取jar文件而抛出加载而不是写在其中的代码......

想法?

谢谢!

3 个答案:

答案 0 :(得分:3)

您可以通过在System.getenv第15行中的downLoadApp.java调用try-catch语句来捕获访问控制异常。也就是说,而不是:

static String tmp = System.getenv("TEMP");

你应该:

static String tmp;

static {
    try {
        tmp = System.getenv("TEMP");
    } catch (java.security.AccessControlException ace) {
        // tmp is not set, maybe use some default value?
    }
}

答案 1 :(得分:2)

你是否在System.getenv("TEMP")类的静态上下文中的某个地方调用downLoadApp?很可能这是在静态字段的初始化器中,但它可能在静态代码块中。

这是失败的地方(抛出java.security.AccessControlException)。堆栈跟踪的其余部分是由此产生的错误。您需要捕获此异常以检测您没有适当的权限。

由于无法捕获静态字段初始值设定项的异常,因此需要将对getenv的调用移动到方法或静态代码块中。

答案 2 :(得分:-1)

我不认为您可以捕获此异常,因为JVM在加载applet时会抛出此异常。