Does a Java applet have to contain a "main" method or does it run from the "init()" method. I have written a small game in java following and example in a book however it does not tell you how to run the damn thing. Code has no erros from what I can see.

Someone explain what i need to do


Arghh another problem...not my day :/

Doesn't load and has an error message

java.lang.UnsupportedClassVersionError: Asteroids : Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at Source)
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$ Source)
    at Source)
Exception: java.lang.UnsupportedClassVersionError: Asteroids : Unsupported major.minor version 51.0

I have little experience but I'm sure someone will correct me if I'm wrong.

The init() method just initializes the applet -- it starts it up.

Try making your applet like this:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

public class applet extends JApplet
    implements Runnable, ActionListener

    static final long serialVersionUID = 1;

    boolean threadRunning = true;
    Thread thread;

    int delay = 20;
    Timer tim;

    public void paint(Graphics g)
        //this method redraws the window when it's called

    public void actionPerformed(ActionEvent e)
        //this method is called every time the Timer goes off (every 20 milliseconds)
            //you have to be careful, though, because this method is also called by other things, in which case you have to test "if(e.getSource() instanceof Timer)"

        /*put something here, like a calculation you want repeated or "repaint();"*/

    public void init()  //this will be the first thing to be called <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        tim = new Timer(delay, this);

        thread = new Thread(this);
    public void destroy()   //this is called when you want to stop your applet
        threadRunning = false;  //setting this to false breaks the while() loop in the next method...
        thread = null;
    public void run()
            //anything you put here is also called again and again

            /*put something here, like a calculation you want repeated or "repaint();"*/

                Thread.sleep(1);    //this pauses the game for some 1 millisecond
            catch(InterruptedException e)

I prefer putting the calculations in the actionPerformed() portion because the time between calculations is the same on all computers (I think). On the other hand, the run(){while()} approach doesn't start the delay until after your calculation.

More stuff:
To answer your actual question, no, I don't think applets need a main least not to test in Eclipse. They might need one if you're programming for webpages, but I'm clueless about that stuff. If you want to create a Runnable Jar File (.jar), you will need a main method somewhere, because that is where all programs start.

Edited 4 Years Ago by kedxu: moar

Unsupported major.minor version 51.0

There is a version problem. The java plugin is older than the version of javac used to compile the class file. I think 51 is java 1.7. Look in the browser's java console window to see what version the plugin is.

Edited 4 Years Ago by NormR1

Thats how the applet has been written. It's not my code im just using the books example to learn more about java but i can't work out how to get it to execute and load properly

Arghhh I don't like java. I've updated java for my browser but now it's coming up with a "wrong name:" error.

I had a search and some people are saying that this is because you have to include the package name before the main class name.

Is this true?

The full class name includes the package. For example: packname1.packname2.classname

Edited 4 Years Ago by NormR1

Ok. So if my project is called "Asteroids" which has a package "asteroids" and in that package the main class is called "Asteroids" would that require:


in order to work in the <applet> tag

The .class at the end is not part of the class name. Sometimes it works and sometimes not, depending on the browser.

Ok this is the tag I used in the html file and it has a "class not found" error

<applet code="asteroids.Asteroids" width=640 height=480> My Applet </applet>

I just can't understand why it will not work. (I also tried the "asteroids.Asteroids.class" but that didnt work either. same erro)

Is the class file in the asteroids folder and is the asteroids folder in the same folder as the html file?

What error messages are shown in the browser's java console?

ok so I have a folder called ast which contains

Asteroids.class (main class)


The html file contains


    <applet code="asteroids.Asteroids" width=640 height=480> My Applet </applet>


The Asteroids class file must be in a folder named: asteroids (the package name).
All class files must be in folders that matches their package name.
The html file should be in the folder that contains the asteroids folder.

Edited 4 Years Ago by NormR1

Done. Now I get a RuntimeException error "java.lang.reflect.InvocationTargetException"

Sorry for all these problems. But thank you for helping

You need to copy the full text of the error message and paste it here.
It shows where the error happened and what methods were called.

There is no message displayed in the java console. It just comes up with the runtimeException alert but when I click details the console loads with only the command help text.

Java Plug-in
Using JRE version 1.7.0_09-b05 Java HotSpot(TM) Client VM
User home directory = C:\Users\Bill
c:   clear console window
f:   finalize objects on finalization queue
g:   garbage collect
h:   display this help message
l:   dump classloader list
m:   print memory usage
o:   trigger logging
q:   hide console
r:   reload policy configuration
s:   dump system and deployment properties
t:   dump thread list
v:   dump thread stack
x:   clear classloader cache
0-5: set trace level to <n>
This article has been dead for over six months. Start a new discussion instead.