java - 为什么我在浏览器中得到 java.lang.reflect.InitationTargetException 而在 Eclipse Applet 查看器中却没有?

标签 java eclipse applet invocationtargetexception

就像标题所说:为什么我在浏览器中得到 java.lang.reflect.InitationTargetException 而在 Eclipse Applet Viewer 中却没有?当我在浏览器中使用 html 加载小程序(是的,我知道小程序已经过时)运行我的小程序时,我收到 java.lang.reflect.InitationTargetException 错误。我读到,当 HTML 尝试加载不是小程序的东西时,就会发生这种情况,但是当我在 Eclipse 中将其作为小程序运行时,它会起作用(调出 Eclipse Java 小程序查看器并且工作完美......这就是让我困惑的地方) 。这是堆栈跟踪:

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.runOnEDTAndWait(Unknown Source)
    at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.instantiateApplet(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.initAppletAdapter(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
    at com.sun.deploy.uitoolkit.impl.awt.OldPluginAWTUtil.invokeAndWait(Unknown Source)
    ... 5 more
Caused by: java.security.AccessControlException: access denied ("java.io.FilePermission" "hq.gif" "read")
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:372)
    at java.security.AccessController.checkPermission(AccessController.java:559)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
    at sun.plugin2.applet.AWTAppletSecurityManager.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkRead(SecurityManager.java:888)
    at sun.awt.SunToolkit.getImageFromHash(SunToolkit.java:774)
    at sun.awt.SunToolkit.getImage(SunToolkit.java:790)
    at sun.lwawt.macosx.LWCToolkit.getImage(LWCToolkit.java:471)
    at javax.swing.ImageIcon.<init>(ImageIcon.java:147)
    at javax.swing.ImageIcon.<init>(ImageIcon.java:174)
    at Application.Game.<init>(Game.java:97)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at java.lang.Class.newInstance(Class.java:374)
    at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter$1.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:302)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

我的文件结构是这样的:

index.html
java.policy.applet
hq.gif
b.gif
k.gif
Application (this is the package)/
     Game.java
     Game.class
     ETC

我的java.policy.applet

grant {
  permission java.security.AllPermission;
};

最佳答案

InvocationtargetException is a generic one. The actual exception is 

Caused by: java.security.AccessControlException: access denied ("java.io.FilePermission" "hq.gif" "read")

我的假设是,除了签名的 jar 之外,您在运行小程序时还需要管理员权限。

您能否合并以下代码,如所述here

AccessController.doPrivileged(new PrivilegedAction() {
    public Object run() {
        // perform the security-sensitive operation here
        return null;
    }
});

关于java - 为什么我在浏览器中得到 java.lang.reflect.InitationTargetException 而在 Eclipse Applet 查看器中却没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23752595/

相关文章:

java - Android 中的 main() 在哪里?

Java - ClassFileTransformer.transform(...) 并发调用相同的 classLoader className 对

java - Web 服务 Glassfish 中的 JDBC

eclipse - 从git导入多个maven项目到eclipse

java - 我如何知道哪个类来自接口(interface)

java - 无法让我的 java applet 游戏运行 NoClassDefFoundError

package - 为什么我能够重新创建 java.lang 包和类?

java - 无法安装 m2e 生命周期映射

JAVA:找不到符号:方法绘制

java - 是否有 HTML5 WebSockets 的 Java 小程序实现?