Intro B

In order for your user to see your applet, they must own a "Java Virtual Machine" or JVM. Don't be worried, however, JVM's exist in most computing devices. Certainly all modern browsers have a version of the Java Virtual Machine in them. Older browsers have older JVM's and newer browsers have... What you do need to be aware of is that most browsers to not boot up with the JVM turned on. So, the first applet the browser sees as it decodes the html opens up the JVM. Once the JVM is open it remains open for the life of that browser useage. Opening up the Java Virtual Machine takes a little time so I like to put my first applet on a site at the bottom of the page much like large images so the reader can read other things while waiting. Technically the applet runs in the JVM not the browser, so there are 2 things you need to create to make a Java applet work on your webpage:
  1. You need to tell the browser to open up JVM and read a Java .class file. That is easy to do. You use an <applet> tag. Here is the html code for displaying a simple applet called HelloWorld.class:
    <HTML> <HEAD> <TITLE>insert your title here </TITLE> </HEAD>
    <BODY> Put your applet tag anywhere you want. It works much like an image tag.
    Let's put it here.
    <applet code="HelloWorld.class" width=200 height=40> </applet>
    <a href=javaintrob.htm#spot>Back to Java Intro </a>
    </BODY> </HTML>
  2. And, you need to put the Java .class file in the same folder as your html document.(Note: You can put it in different folders and specify the path to that folder in the applet tag, it's just easier to put the .class file in the same folder.) You can download a Java.class file from the internet or you can create them yourself. When you put it in the same folder and open up the html document in a browser, the html displays and when you get to the <applet> tag, the .class file is opened and run in that space on the html document. The rest of the html document then finishes displaying. The end result of the two documents(.html and .class) looks like this: Above Java Applet
As you can see, the html code part is easy. It's just another html tag, the <applet> tag. You can add attributes inside the <applet> tag much like you do in an <img> tag. Attributes like "align=", "height=", width=", "hspace=", and "vspace=" all work in <applet> tags just like in images. Later, we will also learn how to pass parameters into an applet between the opening applet tag(<applet>) and the closing applet tag(</applet>). But, for now, basically treat an <applet> tag like an image tag.

The harder part to get is the .class file. The class file is the code that tells the machine's operating system language how to run the applet. It is written in a language only machines can understand. Here is the machine language(.class file) for the above applet.


While certain words are readable by humans much of it is nonsense to us. Each black rectangle, , represents a command that could not be read by a text editor(in this case, Notepad). In other words... machine language not human language. So, you as a programmer, could not write this language it has to be written by a machine for a machine. The code above, a .class file, is not what you will be writing during this trimester. You will, instead be writing a human language that will be compiled by a piece of software called a compiler into the machine language of the .class file. The file you will write in a human language, Java, will be called a .java file. Here is the .java file:
Now the language may be new and strange looking to you, but it is a human programming language. Once you learn the syntax of it(like you did with html and javascript), it is understandable. The .java file is what you will be creating. You will not load it to the webserver however. The .java file is first compiled into a .class file. It is the .class file that you reference in your html and that runs your applet. So, in review, the 3 steps to using Java on your webpages are:
  1. Create a .java file
  2. Compile the .java file into a .class file
  3. Put the <applet> tag into your html document.



Index Grading Schedule Syntax JavaDocs Video Projects Links