Exporting an Applet with Eclipse

Hi all!

Happily working my way through Java 1.4 Game Programming when i hit a stump. The book describes methods to create a .jar file with command line entry and run it with an HTML file, however I have been following along using Eclipse.

This is the java class.


package game.programming.tutorial;

import java.awt.*;
import javax.swing.*;
   
public class MyApplet extends JApplet
{

	public void init()
    {
       setSize(400, 300);
    }
 
    public void start()
    {
 
    }
   
    public void paint(Graphics g)
    {
       g.drawString("Text to Screen", 20, 20);
    }
   
    public void stop()
    {
   
    }
   
    public void destroy()
    {
   
    }
}

This is the HTML file.


<HTML>
   
<HEAD>
    <TITLE>Applet JAR Example</TITLE>
</HEAD>
   
<BODY>
    <CENTER><B>Applet JAR Example</B>
    

    <APPLET CODE="MyApplet.class" ARCHIVE="MyApplet.jar" 		
        WIDTH=400
       	HEIGHT=300>
    </APPLET>
    </CENTER>
</BODY>
   
</HTML>

When I export from eclipse I do the following…
-Right click class(context: Export)
-Select JAR file.
Next
-Resources to export (MyApplet.java is the only one ticked)
-Export generated class files and resources ticked
-compress the contents of the JAR file ticked
Next
-Export class errors ticked
-Export class warnings ticked
Next
-Generate the Manifest ticked
-Seal some packages ticked
Finish

At this point I have a folder on my desktop “AppletTest”
This contains,
AppletTest.html
MyApplet.jar

Double clicking the html file loads in my web browser as expected however the applet window just states,
Error: click for details, which show nothing upon clicking.

Any help would be much appreciated,
Thank You.

Do you see the Java icon in your taskbar? Right click, choose “Open 1.6.0_25 Console” and report back any errors you see.

Aha, I just noticed, your class is in a package. Change the HTML applet tag to:


<applet code="game/programming/tutorial/MyApplet.class" archive="MyApplet.jar" width="400" height="300"></applet>

try changing

APPLET CODE="MyApplet.class"

to

APPLET CODE="game/programming/tutorial/MyApplet.class" 

It works!

Thank you.

Glad to help :slight_smile: