1

当我尝试运行小程序时出现以下异常:

java.lang.reflect.InvocationTargetException
at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source)
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.RuntimeException: java.lang.IllegalAccessException: Class sun.plugin2.applet.Plugin2Manager$12 can not access a member of class MyApplet with modifiers ""
at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(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.lang.IllegalAccessException: Class sun.plugin2.applet.Plugin2Manager$12 can not access a member of class MyApplet with modifiers ""
at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
... 9 more

异常:java.lang.reflect.InvocationTargetException

小程序代码:

import javax.swing.*;
import java.awt.*;

class MyApplet extends JApplet {

public JFrame frame;
public JPanel panel;
public JButton button;

public void init() {
    frame = new JFrame();
    panel = new JPanel();
    button = new JButton("click me ");
    panel.setBackground(Color.RED);
    panel.add(button);
    frame.add(panel);
    frame.setSize(300,300);
    frame.setVisible(true);
}   
 }

HTML 文件:

<applet code="MyApplet" codebase="AppletPackage" archive="JAR.jar" height="800" width="800">

JAR.jar 包含一个名为 AppletPackage 的包,其中包含 MyApplet 类。它还包含自动添加的清单文件。

问题是什么 ?

4

1 回答 1

3

AnInvocationTargetException表明某些反射调用失败。似乎某些 Sun (Oracle) 类正在尝试使用反射来访问代码中的类。

您的课程MyApplet没有public范围修饰符。如果你添加它,我希望事情会奏效,因为这就是异常所抱怨的。

原因:java.lang.IllegalAccessException:类 sun.plugin2.applet.Plugin2Manager$12无法访问带有修饰符“”的 MyApplet 类的成员

于 2012-07-09T13:32:30.737 回答