title: Creating and running a Java applet
body: <p>I thought I would try my hand at applets - I made an applet using Eclipse. It runs fine using the Run As -> Java Applet.</p>
  
  <p>I read a bit about running it outside Eclipse, so I did the following:</p>
  
  <ol>
  <li>Made a folder.</li>
  <li>Created New -> Java Project [applet_test].</li>
  <li>Inside the project, I created New -> Other -> Visual Swing Class -> Applet [Number1] - that created <code>Number1.class</code>.</li>
  <li>Added code and ran it as a Java applet - it ran fine.</li>
  <li>Exported the project as a <a href="http://en.wikipedia.org/wiki/JAR_%28file_format%29" rel="nofollow">JAR</a> file  (not a runnable JAR file).</li>
  <li>Wrote HTML using <a href="https://en.wikipedia.org/wiki/TextEdit" rel="nofollow">TextEdit</a> (Mac's version of Windows' <a href="http://en.wikipedia.org/wiki/Notepad_%28software%29" rel="nofollow">Notepad</a>). The HTML follows, below...</li>
  <li>I put the JAR file, HTML and .class file in the folder.</li>
  <li>In Terminal (Mac's version of Windows command prompt window), I ran Appletviewer applet_testX2.html  (that's the name of my HTML).</li>
  <li>I could see a brief flash of the application name at the top of the screen (as would any other running application).</li>
  </ol>
  
  <p>However, the application (which should display a Jpanel with a label and a button) did NOT appear. I also tried running it from Firefox and Safari. Only the HTML code appeared.</p>
  
  <p>So, what am I doing wrong? And, more importantly, how do I do it correctly?</p>
  
  <p>Code follows without imports statements:</p>
  
  <pre><code>&lt;html&gt;
      &lt;body&gt;
          &lt;applet code="Number1.class" archive="applet_test.jar"
              width=300
              height=300&gt;
          &lt;/applet&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The Java code:</p>
  
  <pre><code>public class Number1 extends JApplet {
      public Number1() {
  
      }
  
      private static final long serialVersionUID = 1L;
  
      @Override
      public void init() {
          try {
              EventQueue.invokeAndWait(new Runnable() {
                  @Override
                  public void run() {
                      initComponents();
                  }
              });
          }
          catch (Exception ex) {
              ex.printStackTrace();
          }
      }
      private void initComponents() {
          setSize(320, 240);
  
          JPanel panel = new JPanel();
          getContentPane().add(panel, BorderLayout.CENTER);
  
          JLabel lblAppletTest = new JLabel("Applet test 1");
          panel.add(lblAppletTest);
  
          JButton btnPushIt = new JButton("Push it");
          panel.add(btnPushIt);
      }
  }
  </code></pre>
  
  <p>Firefox source view:</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
          &lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
          &lt;title&gt;&lt;/title&gt;
          &lt;meta name="Author" content="BT"&gt;
          &lt;meta name="Generator" content="Cocoa HTML Writer"&gt;
          &lt;meta name="CocoaVersion" content="1038.35"&gt;
          &lt;style type="text/css"&gt;
              p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Helvetica}
              p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Helvetica; min-height: 19.0px}
          &lt;/style&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;p class="p1"&gt;&amp;lt;html&amp;gt;&lt;/p&gt;
          &lt;p class="p1"&gt;&lt;span class="Apple-converted-space"&gt; &lt;/span&gt;&amp;lt;body&amp;gt;&lt;/p&gt;
          &lt;p class="p1"&gt;&lt;span class="Apple-converted-space"&gt;   &lt;/span&gt;&amp;lt;applet code="Number1.class" archive="applet_test.jar"&lt;/p&gt;
          &lt;p class="p2"&gt;&lt;br&gt;&lt;/p&gt;
          &lt;p class="p1"&gt;&lt;span class="Apple-converted-space"&gt;    &lt;/span&gt;width=300&lt;/p&gt;
          &lt;p class="p1"&gt;&lt;span class="Apple-converted-space"&gt;   &lt;/span&gt;height=300&amp;gt;&lt;/p&gt;
  
          &lt;p class="p1"&gt;&amp;lt;/applet&amp;gt;&lt;/p&gt;
          &lt;p class="p1"&gt;&amp;lt;/body&amp;gt;&lt;/p&gt;
          &lt;p class="p1"&gt;&amp;lt;/html&amp;gt;&lt;/p&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>