Why do I get java.lang.reflect.InvocationTargetExc

2019-09-03 06:24发布

问题:

Just like the title says: Why do I get java.lang.reflect.InvocationTargetException in browser but not in eclipse applet viewer? When I run my applet (yes, i know applets are outdated) in the browser using html to load it, i get the java.lang.reflect.InvocationTargetException error. I read that this happens when the HTML is trying to load something that is not an applet, but it works when I run it as an applet in eclipse (brings up eclipse java applet viewer and works perfect...that is what stumps me). Here is the stack trace:

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)

My file structure is like this:

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

My java.policy.applet

grant {
  permission java.security.AllPermission;
};

回答1:

InvocationtargetException is a generic one. The actual exception is 

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

What I assume is, you would need admin priviledges while running the applet in addition to the signed jar.

Can you incorporate the following code, as described here

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