HTML中嵌入的Java Applet导致InvocationTargetException

时间:2014-03-05 06:41:20

标签: java file applet invocationtargetexception

我有以下HTML用于嵌入我的Java小程序:

<applet code="MyClass" URL="path/MyClass.class" height="200" width="400"></applet>

小程序不起作用,我在控制台中收到以下错误:

  

java.lang.reflect.InvocationTargetException       at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(DeployAWTUtil.java:116)       at sun.plugin2.applet.Plugin2Manager.runOnEDT(Plugin2Manager.java:3606)       at sun.plugin2.applet.Plugin2Manager.createApplet(Plugin2Manager.java:3131)       at sun.plugin2.applet.Plugin2Manager $ AppletExecutionRunnable.run(Plugin2Manager.java:1517)       在java.lang.Thread.run(Thread.java:680)   引起:java.lang.ClassCastException:MyClass无法强制转换为java.applet.Applet       at sun.plugin2.applet.Plugin2Manager $ 13.run(Plugin2Manager.java:3119)       在java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:199)       at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:715)       at java.awt.EventQueue.access $ 400(EventQueue.java:82)       at java.awt.EventQueue $ 2.run(EventQueue.java:676)       at java.awt.EventQueue $ 2.run(EventQueue.java:674)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.AccessControlContext $ 1.doIntersectionPrivilege(AccessControlContext.java:86)       at java.security.AccessControlContext $ 1.doIntersectionPrivilege(AccessControlContext.java:97)       at java.awt.EventQueue $ 3.run(EventQueue.java:690)       at java.awt.EventQueue $ 3.run(EventQueue.java:688)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.AccessControlContext $ 1.doIntersectionPrivilege(AccessControlContext.java:86)       at java.awt.EventQueue.dispatchEvent(EventQueue.java:687)       at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)       at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)       at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)       at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)       at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)       在java.awt.EventDispatchThread.run(EventDispatchThread.java:122)   例外:java.lang.reflect.InvocationTargetException

造成这种情况的原因是什么?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

看到你的错误,我看到了:

Caused by: java.lang.ClassCastException: MyClass cannot be cast to java.applet.Applet 

可能JVM插件没有正确加载你的课程,我认为你必须使用存档参数而不是 URL 才能指定你的applet类的路径,如果是 code 参数,则必须指定完整路径mypackage.MyClass,并确保applet主类扩展javax.swing.JApplet或java.applet.Applet。

我还建议你使用&lt;对象&gt;和&lt;嵌入&GT;而不是&lt;小应用程序&GT;因为在html4html5规范中已弃用:

希望这有帮助,