Results 1 to 6 of 6
  1. #1
    pentad is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default 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:

    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:

    Java Code:
    <applet code="HelloWorld.class" archive="HelloWorld.jar"</applet>
    Here is the Java code:

    Java 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();
    	}
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,463
    Rep Power
    25

    Default

    java.lang.ClassCastException: HelloWorld cannot be cast to java.applet.Applet
    You should read the Java tutorial about how to write an applet. The class must extend an applet class and the methods called by the browser are different from an application. Your code appears to be an application: Doesn't extend an applet class and has a main() method.

    Also the HTML entry should have a width and height tag.

    See: Lesson: Applets (The Java™ Tutorials > Deployment)

  3. #3
    pentad is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    Norm,

    Thank you for the quick reply. Two quick questions:

    1. Is there a way to run a Java Application via a web browser? Our company has a Java based application that would be nice to run via web browser.

    2. You mention applet but hasn't that be deprecated? Should I pursue information on that or the object tag?

    Thanks!
    -P

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,463
    Rep Power
    25

    Default

    1) Look at JNLP. It may have something to help.

    applet but hasn't that been deprecated
    I don't know.
    I used applet in the generic sense. Otherwise I would have said the Applet class.
    Are you talking about a java class or HTML?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,463
    Rep Power
    25

    Default

    Duplicate deleted.

  6. #6
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Is there a way to run a Java Application via a web browser? Our company has a Java based application that would be nice to run via web browser.
    No. But you can make an program that is both an application and an applet. It takes only minor adjustments. (But an applet has restrictions. It depends on the app how bad that is.)
    You mention applet but hasn't that be deprecated? Should I pursue information on that or the object tag?
    Only the HTML applet-tag seems to be depricated. JApplet as Java-class is not.
    No bug ever had to calculate its fitnessfunction.

Similar Threads

  1. Java applet problem
    By rasikh in forum Java Applets
    Replies: 6
    Last Post: 11-05-2012, 11:55 AM
  2. Java applet size problem
    By madnaelo in forum Java Applets
    Replies: 1
    Last Post: 12-06-2010, 01:39 AM
  3. java applet button problem .. :( plz help!
    By i4gotmyid in forum Java Applets
    Replies: 1
    Last Post: 04-17-2008, 04:11 PM
  4. Java Applet re-creation problem
    By Mikalai.Kardash in forum Java Applets
    Replies: 0
    Last Post: 07-23-2007, 02:09 PM
  5. Problem with run Java Applet
    By Albert in forum Java Applets
    Replies: 1
    Last Post: 07-13-2007, 03:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •