Results 1 to 12 of 12
  1. #1
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default Please help: just cannot get my first ever applet to run

    This just won't run:
    Java Code:
    public class SkeletonJApplet extends JApplet {
    
        public void init() {
            AnAppletWorld myWorld;
            
            myWorld = new AnAppletWorld("My World");
            myWorld.setVisible(true);
        }
    }
    where, in a separate file, I have this:
    Java Code:
    public class AnAppletWorld extends JFrame {
        
        public AnAppletWorld(String title){
            super(title);
            this.setSize(250, 200);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        
    }
    Here is my HTML page:
    Java Code:
    <!doctype html>
    <html lang="en">
    <head>
    	<title>java applet effort</title>
    	<meta charset="UTF-8">
    	<style>
    		canvas { border: 1px solid black;}
    	</style>
    </head>
    <body>
    <p>HALLO FROM java applet effort!! </p>
    <p>**********</p>
    <p>**********</p>
    <applet code="SkeletonJApplet.class" width=600 height=450>
    </applet>
    </body>
    </html>
    And here is the result I get:
    Please help: just cannot get my first ever applet to run-error-message.jpg

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Please help: just cannot get my first ever applet to run

    Sounds like an installation problem. Did you try a different browser just incase?

  3. #3
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default Re: Please help: just cannot get my first ever applet to run

    Thanks for replying.

    Yeah, I get the same problem no matter what browser I try: firefox, ie, gchrome.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Please help: just cannot get my first ever applet to run

    You can't show a JFrame.
    Your JApplet is your top level component.

    What's happening is your code is trying to open a JFrame (a new window), and applets are not allowed to do that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default Re: Please help: just cannot get my first ever applet to run

    I'll change "AnAppletWorld extends JFrame" to "AnAppletWorld extends JPanel", then - see how that goes.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Please help: just cannot get my first ever applet to run

    You will need to add it to your applet.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default Re: Please help: just cannot get my first ever applet to run

    Really getting fed up now.

    Okay, here's my code:
    Java Code:
    import javax.swing.JApplet;
    import javax.swing.*;
    
    public class SkeletonJApplet extends JApplet {
    
        public void init() {
            AnAppletWorld myWorld;
            
            myWorld = new AnAppletWorld();
            myWorld.setVisible(true);
            add(myWorld);
        }
    }
    
    import javax.swing.*;
    import java.awt.event.*;
    
    public class AnAppletWorld extends JPanel {
        private JButton exitButton = new JButton("Exit");
        
        public AnAppletWorld(){
            this.setSize(250, 200);
            this.add(exitButton);
            exitButton.addActionListener(new ExitButtonWatcher());
        }
        
        private class ExitButtonWatcher implements ActionListener{
            public void actionPerformed(ActionEvent a){
              System.exit(0);
            }
        }
        
    }
    And here's my HTML file:
    <!doctype html>
    <html lang="en">
    <head>
    <title>java applet effort</title>
    <meta charset="UTF-8">
    <style>
    canvas { border: 1px solid black;}
    </style>
    </head>
    <body>
    <p>HALLO FROM java applet effort!! </p>
    <p>**********</p>
    <p>**********</p>
    <applet code="SkeletonJApplet.class" width=600 height=450>
    </applet>
    </body>
    </html>

    And here's my new error message.
    Please help: just cannot get my first ever applet to run-err-msg-2.jpg

    I'm STUCK. Please help. Thanks.

  8. #8
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Re: Please help: just cannot get my first ever applet to run

    I am not that good with html, but I am pretty sure that I know a start to finding out your problem. You should find out if it's your html or java that is faulty. Considering that it says noClassDefFoundError, I would geuss that it is most likely just your html not pointing to the right file.
    Like I said, I don't know much about html, but this is a start. Hope it helps!

  9. #9
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default Re: Please help: just cannot get my first ever applet to run

    Thanks for the feedback. I'll think about that, and how I might proceed from here.

    I take your point; but then, what's puzzling is that the class that it's apparently not finding is the inner private one, ExitButtonWatcher - suggesting that it's getting started okay in finding my SkeletonJApplet class. If it is finding that starting class okay, then I don't know how to 'help' it find subsequent classes.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Please help: just cannot get my first ever applet to run

    Look in the directory where the .class file is generated.
    You will see there is more than one.
    You need to add both into your applet tag (and on your server).
    When the compiler runs it generates a class file for all classes. This includes any inner classes (as in your case), and any anonymous classes (they would be <clasname>$n where n is a number).

    Also, as a code thing, you need to add your component to the applet before making it visible.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default Re: Please help: just cannot get my first ever applet to run

    I changed my HTML like this:

    <applet code="SkeletonJApplet.class" code="AnAppletWorld.class" width=600 height=450>
    </applet>

    But that made no difference.

    Are you saying I should be including every .class file in my applet tag - all the inner ones and all the anonymous ones?

    Thanks.

    (Also, should I be using an <object> tag instead - my understanding is that that is meant to be better practice?)

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Please help: just cannot get my first ever applet to run

    Yes, you need all class files that were created by the compiler.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 05-18-2012, 06:06 PM
  2. Replies: 30
    Last Post: 08-26-2011, 09:05 PM
  3. applet working in applet viewer, not browser
    By plasticfood in forum Java Applets
    Replies: 4
    Last Post: 06-24-2011, 06:47 PM
  4. Calling another applet on click of button in one applet
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 1
    Last Post: 02-19-2009, 12:54 PM
  5. Replies: 4
    Last Post: 06-08-2007, 06:15 AM

Posting Permissions

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