NetBeans Hello World Applet

I’m pulling my hair out here. I’ve followed tutorials on the NetBeans website on creating a simple hello world applet using NetBeans and it just won’t work in the browser. The applet viewer built into NetBeans displays just fine, however when I embed in an HTML file I get in the status bar:

“Applet HelloWorld.jar notloaded”

and when I mouse over the applet I get

“Loading Java Applet Failed…”

When I attempt to load from the command line (although this is an applet not an application so I’m not sure this would work anyway) I get a “Failed to load Main-Class manifest attribute” error. So, I added a new class with an empty main method to my applet project using:


public static void main(String args[]) {

}

However, I’m aware that applets do not have main methods to initialize (they use init). So basically, I have no idea why I can’t run a darn simple HelloWorld applet in my browser. It’s making me nuts lol. I’ve read on Sun’s site about creating jnlp files, etc, however I’m assuming that NetBeans will package this in the Jar file for me? Any help would be greatly appreciated.

Thanks!

(oh and I’m using a Mac, Firefox and have the latest version of Java on my machine and I’m using NetBeans version 6.7.1)

I figured it might be useful if you had the NetBeans project too…here it is attached…

You are correct there is no main method in an applet. You can’t run an applet from the command line using “java”, there’s another executable called appletviewer, use that. For the web browser the embedded object probably has the wrong path. Can you post the contents of the HTML file used?

Here’s the HTML code:



<html>
<body>
<applet code="greenarrow.main.HelloMain.class" archive="HelloApplet.jar" width="500"
                                                                height="500"></applet>

</body>
</html>


The JAR file is in the same directory as the HTML file. Also, I am using swing so I do have a subdirectory called “lib” with a “swing-layout-1.0.3.jar” file. Any ideas?

Applets don’t start running at a main method, the first thing called in an applet it the start() method, you’ll also want a paint() method, if you want to draw anything to the screen. Lookup Applet in the JDK API, for the methods you need to overload.

public class MyApplet extends Applet {

public MyApplet(){
// init variables
}

public void start(){
// any load time start up
}

public void paint(Graphics g){
g.drawString(10,20,“Hello World”);
}

}

badams67 - I’m using Swing in my applet (JApplet) though. I’m aware of the methods that I can override (start, etc.), however using a JApplet I thought I don’t need to override these methods. Am I incorrect in this assumption? Someone please let me know what I’m doing wrong…I appreciate the help I’ve received thus far though…I provided an attachment but it hasn’t been approved yet…I posted this morning so I’m not too sure what’s taking so long… http://www.sitepoint.com/forums/attachment.php?attachmentid=53135&d=1255530727

Try removing the .class extension in the code attribute.

You don’t need to override those methods unless you want to do something specific that the default implementation doesn’t do. For now the default implementation should work fine.

rozner - I tried removing the .class and that doesn’t work either. Above I provide a link to my source code. If you have a sec drop it into NetBeans and check it out. Still not sure what’s up…

Are the HelloApplet.jar referenced in your applet tag and the HelloApplet.zip you posted one in the same?

If so, upon opening the archive, you’ll note that there is no class file in greenarrow/main.

Alright, just disregard what I said before…

Netbeans doesn’t provide you with what you need to actually deploy this, to my knowledge.

You will need: an html with the applet tag, a jar with your code in it, the layout library you reference in your code.

Let’s say you are working out of a directory named applet, the contents of applet will look like this (the contents and structure of MyApplet.jar are shown for clarity):


applet
|   applet.html
|   swing-layout-1.0.3.jar
|   
\\---MyApplet.jar
    +---greenarrow
    |   \\---main
    |           HelloMain$1.class
    |           HelloMain$2.class
    |           HelloMain.class
    |           HelloMain.form
    |           HelloMain.java
    |           
    \\---META-INF
            MANIFEST.MF

You will build MyApplet.jar, yourself, by changing to the root of your project’s directory and entering the following command (the jar command will build and add the manifest file for you):

jar cvf MyApplet.jar .

Note: the given the below structure, the root of your project is dev


dev
+--greenarrow
    +--main

applet.html should look like this, note that both your jar and the layout jar are referenced:


<html>
<body>
<applet code="greenarrow.main.HelloMain.class" 
        archive="MyApplet.jar,swing-layout-1.0.3.jar" 
        width="500"
        height="500"></applet>
</body>
</html>

hth

rushiku - worked like a charm! Thank you so much - I appreciate it! Is possible to bundle the Swing library with the MyApplet.jar so I’m just referencing the one file (MyApplet.jar)?