How to make Applet in NetBeans?

I need to create applet for Java4kb competition. Basicly, what should I click in NetBeans IDE to make one of these templates “compile” (I’m getting “can’t find main()”)?

And yes, I was unable to google it, which is kind of strange since something that basic should be covered somewhere…

Applets don’t have main methods. Netbeans should automatically run your applet in the AppletViewer when you hit the run button. What problems did you encounter?

NetBeans 7.1

Let’s say I want to run this simple HelloWorld code from wikipedia:


import java.applet.Applet;
import java.awt.*;
 
// Applet code for the "Hello, world!" example.
// This should be saved in a file named as "HelloWorld.java".
public class HelloWorld extends Applet {
  // This method is mandatory, but can be empty (i.e., have no actual code).
  public void init() { }
 
  // This method is mandatory, but can be empty.(i.e.,have no actual code).
  public void stop() { }
 
  // Print a message on the screen (x=20, y=10).
  public void paint(Graphics g) {
    g.drawString("Hello, world!", 20,10);
 
  // Draws a circle on the screen (x=40, y=30).
    g.drawArc(40,30,20,20,0,360);
  }
}

I click:

  • NewProject/Java/JavaApplication
  • Set project name to: “AppletHelloWorld”, Finish
  • click on AppletHelloWorld.java, I see this:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package applethelloworld;

/**
 *
 * @author Admin
 */
public class AppletHelloWorld {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
	// TODO code application logic here
    }
}

  • I copy/past the code and change “public class HelloWorld extends Applet” to “public class AppletHelloWorld extends Applet”
import java.applet.Applet;
import java.awt.*;
 
// Applet code for the "Hello, world!" example.
// This should be saved in a file named as "HelloWorld.java".
public class AppletHelloWorld extends Applet {
  // This method is mandatory, but can be empty (i.e., have no actual code).
  public void init() { }
 
  // This method is mandatory, but can be empty.(i.e.,have no actual code).
  public void stop() { }
 
  // Print a message on the screen (x=20, y=10).
  public void paint(Graphics g) {
    g.drawString("Hello, world!", 20,10);
 
  // Draws a circle on the screen (x=40, y=30).
    g.drawArc(40,30,20,20,0,360);
  }
}

  • click Run, “BUILD SUCCESSFUL (total time: 0 seconds)”, nothing appears on the screen
  • Click “Clean and Build Main Project”, click Run again, get an error “applethelloworld.AppletHelloWorld class wasn’t found in AppletHelloWorld project; select the main class”.

I guess, I clicked something wrong along the way :slight_smile:
I was checking several tutorials but I always stumbled on some differences (like there is no “Run as Applet” option in NetBeans 7.1 or the option they select in the tutorial does not exist)

you can go to the project properties
Application->Web Start
-Enable Web Start
-Applet descriptor

I do it a bit different I create a Component like a Canvas which I can attach to an applet or jframe, depending on how I build it(desktop or applet)

No change, still “class not found”. Also I’m getting now an icon “AppletHelloWorld needs to be compiled”, no matter what I click I can’t make it disappear.

I just opened up Netbeans, wrote a little test applet, and wow I’m getting the same error: “Main class not found.”

Another reason why Eclipse > Netbeans (please let’s not start an IDE war…) ;D

In Eclipse it works like a charm… And I was already so used to the faster launch speed of NetBeans :frowning:

you just have to hit SHIFT+F6(run file) while being in your applet class file in netbeans

Google Chrome’s Java installation messes up netbeans applets.

google chromes java? - what could you possible do with an applet so that it have browser specific behavior
netbeans applets? - why do you think so? what settings
messes up? - what happens?

I made a simple hello world applet in netbeans, didnt work, uninstalled google chrome’s java, recompiled and ran, worked perfectly.

There is no such thing as “Google Chrome’s Java”. There is only 1 Java installation.

Chrome has its own java installation for their browser.

You mean that “chromeinstall.exe” file that you click? All that does is download the JRE for system-wide use…

It still breaks applets when you use netbeans

Well I blame Netbeans :slight_smile: