Ok, well I am JUST beginning to learn Java, and I have run into a little stumbling block. I am attempting to write a little applet, but it doesn't seem to want to run.

My Java code:

import java.awt.*;
import java.applet.*;

public class TestApplet extends Applet {
	public void paint(Graphics g) {
			g.drawString("Please work my little applet!", 20, 30);
	}
}

My HTML:

<HTML>
<HEAD><TITLE>Test Applet</TITLE></HEAD>
<BODY>
<applet code="TestApplet.class" width="300" height ="300">
</BODY>
</HTML>

So...I'm not quite sure where I am going wrong. I receive no errors while compiling and there isn't much code for me to mess up.

Could anybody help me with this problem?

Thanks in advance.

Recommended Answers

All 10 Replies

Read the Java tutorial on applets, here. You are missing a key method every applet needs.

Thanks for the link, except that example is not even working. I copied and pasted the code verbatim and I still get a little gray box of nothing.

Thanks again.

EDIT: Well, this is odd. I am able to view my applet within an IDE but not through a browser (Firefox 3.5). Any ideas to why this is?

EDIT: Well, this is odd. I am able to view my applet within an IDE but not through a browser (Firefox 3.5). Any ideas to why this is?

Not really lol. I never had to integrate them with webpages, I just had to make some for a class once. I'm sure someone else around here will know.

There are a couple of things you can check for this problem:
1. Is your browser set to "Enable Java" ? (Go to Tools > Options > Content Panel
2. Ensure that your applet file (class file) have the appropriate file-access permission and the browser should be able to read it (read/write access to files)
3. Read on security policies that go with your applets when deploying it to a browser.
2. Lastly, Did you get any error messages inside the browser?

hope that helps

commented: Thanks for picking it up :) +6

1. Yes, Java is enabled.
2. Both of the files have read/write/execute access.
3. Sorry, could you explain this a little bit?
4. No, I received no error messages in Linux or Windows.

If you would like to see this for yourself, (in no way am I advertising my website) you can see it here: http://patbriggs.net/mytest.html

Thanks for the ideas.

Hello! Most Applets now extend the JApplet class now. (JApplet is part of the javax.swing package) So you should probably be extending that instead. also, all applets make a call to init(), start(), destroy(), and stop(). The paint() method is located in the Container class. anyways, that's just some background info for applets. You should add in the above methods to your code, because they're called by the browser to start the applet. hope this helps =)

Actually, when I opened your link there was an error message displayed in the java console. If you are using Mozilla you can go to Tools > Java Console. If you are using Internet Explorer , right click on the applet area and select Java Console.

The error was a 'java.lang.NoClassDefFoundError' which is caused when your browser couldn't locate the *.class file of your applet. There are two options you can go about solving this:
1. make sure all your *.class files are in the same directory as to your html file; or
2. use CODEBASE attribute of the APPLET tag in your html file(point the codebase to the directory of where your *.class files of the applet are located)

About the security thing read it here:

http://java.sun.com/developer/onlineTraining/Programming/JDCBook/signed.html

hope that helps :)

Ok, well it's rather hard for me to test as whenever I visit the page with my applet on it, my browser crashes (so all I know is that it doesn't work).

And both my .class file and my .html file are already in the same directory. I have also added a "codebase" element to my applet tag.

Updated code:

<HTML>
<HEAD>
<TITLE>Test Applet</TITLE>
</HEAD>
<BODY>
<applet codebase="/" code="TestApplet.class" width="300" height ="300">
</applet>
</BODY>
</HTML>

Thanks for all the great help. But have any more ideas?

Okay, I would like to start by say I'm sorry if this forum has a strict "no-bump after x days" etiquette, but I found a solution to my problem and I thought I would share. Although I did successfully have the latest JRE installed, I guess I didn't have a Java plugin installed for my browser. Being on Linux, I have no idea why my package manager would install only the JRE and not the browser plugin...oh well.

This forum has no such policy, in fact you can reply to threads from over 5 years ago...if you really wanted too. Thank you for posting the solution to your problem, and for marking the thread as solved.

commented: Very helpful and informative +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.