Problem with my Java Applet
Hello all,
I apologize in advance if I missed this question/answer but I cannot seem to find solution either here or on Google.
Question: I cannot get the following Java program (jar) to work in my html page. I wrote the simple program below in Eclipse. I then exported it as an executable JAR and then put it in the same directory as my html file with the HTML code below.
Thanks for any help!
-P
When I load the page I receive this error:
Quote:
java.lang.reflect.InvocationTargetException
at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Un known Source)
at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unk nown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassCastException: HelloWorld cannot be cast to java.applet.Applet
at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception: java.lang.reflect.InvocationTargetException
Here is the HTML code:
Code:
<applet code="HelloWorld.class" archive="HelloWorld.jar"</applet>
Here is the Java code:
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class HelloWorld {
public static JFrame myframe = new JFrame();
public static JButton testbutton = new JButton();
public static JButton closebutton = new JButton();
public static void main(String[] args) {
kickstart();
}
public static void drawme() {
myframe.setSize(300, 300);
myframe.setTitle("My Test");
myframe.setLocation(150, 150);
myframe.getContentPane().setLayout(null);
myframe.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
myframe.setResizable(false);
closebutton.setText("Exit");
closebutton.setSize(75, 25);
closebutton.setLocation(219, 247);
closebutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
testbutton.setText("Test");
testbutton.setSize(75, 25);
testbutton.setLocation(111, 98);
testbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
test();
}
});
myframe.getContentPane().add(closebutton);
myframe.getContentPane().add(testbutton);
myframe.setVisible(true);
}
public static void test() {
JOptionPane.showMessageDialog(myframe,"Hello World!");
}
public static void kickstart() {
drawme();
}
}