就像标题所说:为什么我在浏览器中得到java.lang.reflect.InvocationTargetException而在eclipse applet viewer中却没有?当我在浏览器中使用html加载它来运行我的applet(是的,我知道applet已经过时)时,我得到了java.lang.reflect.InvocationTargetException错误。我读到当HTML试图加载不是applet的东西时会发生这种情况,但是当我在eclipse中将它作为applet运行时它会起作用(带来eclipse java applet viewer并且工作完美......这就是让我烦恼的事) 。这是堆栈跟踪:
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;
};
答案 0 :(得分:2)
InvocationtargetException is a generic one. The actual exception is
引起:java.security.AccessControlException:访问被拒绝 (&#34; java.io.FilePermission&#34;&#34; hq.gif&#34;&#34;阅读&#34;)
我认为,除了签名的jar之外,在运行applet时还需要管理员权限。
是否可以合并以下代码,如here
所述AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
// perform the security-sensitive operation here
return null;
}
});