I wanted to embed a few java applications I wrote in my web page as java applets. Here are my challenges and my solutions. I use jdk1.8.0_31 and Netbeans 8.0.2

How to convert a java application to java applet.

This was surprisingly very easy. First I imported my java applet class by adding the following code to the list of classes I was importing:

import java.applet.Applet;

Second, the first line of my code that says “public class your_application_name”, I simply extended it as an applet like this:

public class your_application_name extends Applet

Thirdly I changed public static void main(String[] args) to public void init().

And believe it or not THAT’S it! I Compiled it and I had my applet.

Side note #1: For my application that had a Graphical User Interface, it would have been embedded in a JFrame via “your_JFrame.setContentPane(your_GUI_Panel);” In this case I simply inserted my GUI panel into the “add();” code like this:


and removed all of my JFrame code.

Side note #2: Browsing the internet I found many saying an applet needs at least 5 methods: init(), start(), paint(), stop(), destroy(). Well, I have programed perfectly working applets with all of my code placed in the init() method. The other 4 methods are optional. My understanding of the methods being used:
  • If I wanted something to happen when someone leaves my page – perhaps an animation must pause – in this case my pause request would go in the stop() method.
  • When the browser returns to the page, the start() method is activated – so here is where I would want to add my resume code.
  • If I want to “print” a shape or text to the screen. I would perform this function in the paint() method.
  • If my program adds a temporary file onto the computer of the user and I would like to remove it after they exit the browser – well my clean up routine would go into the destroy() method. Learn more here.

How to embed an applet into a webpage.

Again, very easy. I used the code below as a template.

<applet code= your_applet.your_applet.class archive="your_applet.jar"
width=320 height=240></applet/>

I had to make sure I wrote, for archive, the file name correctly down to the extension! I made sure what I wrote for code was written in the CORRECT CASE – otherwise I would have gotten a classNotFoundException!

In fact here is what I saw when I first loaded my webpage:

My screen shots are in German, for an English system this would read: Error- Click for details.

Clicking for details would display:

This dialog box is titled: Application Error. Your options Details, Ignore, Reload.

This error happened to me because testApplet.class should read TestApplet.class – note the case difference.

Archive took the exact file path of my jar (with the appropriate extension). If my jar Was in some website domain, I had to make sure to start with http://. If my jar was locally on my machine, I had to start with file://. If my jar was saved in the same directory as my html file, I would simply write the name of my jar file.

<applet code= your_applet.your_applet.class archive="your_applet.jar" width=320 height=240></applet>

Now for the code – here it got tricky. The code took a class file and NOT the jar file. In fact this class file is found inside of the archive file I just talked about. Let’s call the jar file your_applet.jar. If I simply wrote “your_applet.class” I am telling the system that if the jar were to be extracted, I would see the “your_applet.class” file. Let’s simulate this - here is something interesting I did. I right clicked on my jar file, and used my zip program to extract the files. If I saw “your_applet.class” listed then indeed “your_applet.class” is what I had to put in code. BUT, if I saw a folder called your_applet or whatever else, and then within that folder is “your_applet.class”, then I would have to specify this exact file path! To do this, I did not use forward slashes e.g. “your_applet/your_applet.class”, but I used a full stop like this: “your_applet.your_applet.class”.

Side note: This folder is actually where I “package” my class.

<applet code= your_applet.your_applet.class archive="your_applet.jar" width=320 height=240></applet>

Trying to run the applet.

Now – it is 16 Feb 2015 as I write this article. And these days when I open my browser and run the webpage, my applet is embedded on – here is was supposed to get. (My screen shots show German prompts which, I will translate to the exact text you would find if it were displayed in English).

English: Activate Java First a prompt to activate the Java Pluggin. (I selected allow to grant access – one time or always)

A second window pops up that gave me the option to “Allow Now” or “Allow and Remember”. If I selected allow now, every time I load my webpage I would get this security prompt. Allow and Remember of course prompts once and for all.

Here are some error messages along with their icons so I could have easily seen what java was trying to tell me.

The results:

In English this reads: Title: Java Application Blocked. For security, applications must now meet the requirements for the High or Very High security settings, or be part of the Exception Site List, to be allowed to run. More Information. Name: testapplet.TestApplet, Location: file://, Reason: Your security settings have blocked an application signed with an expired or not yet valid certificate from running. Option “OK”.

Not a very pleasant surprise.

Now I said suppose to – so what actually happened? My fire fox browser froze, or hanged! If you are having this same problem, go to Java to run an applet designed by java themselves. If your applet hangs – consider my solution. I was forced to conclude that something was wrong with my firefox/machine. Infact it could have been anything from virus to malware.

The solution – I created a new user account on my computer. My Firefox was clean as a whistle and my applets stopped hanging! Now back to my unpleasant surprise.

NEXT: Background information on digital signing.

Site Links

Fleamarket (en)
Flohmarkt (de)
Recipes (en)
Rezept (de)
Experiments (en)
Experimenten (de)
Home Repair(en)
Heimreparatur (de)
Aufsätze (de)
Java Applets(en)/(de)

Miscellaneous (en)
Sonstiges (de)