Create Software Components 

Using Java Level 2

 

 

Course Info

Scheme

Resources

Tutorials

Java Demos

Utilities

Links


 

   Lecture 1

First Steps

 

Writing your first program

There are two types of Java programs, applets and applications.  Applications are stand-alone programs that operate like any other applications, for instance Notepad.  Applets are programs that run inside a Java enabled web browser, such as Internet Explorer or Netscape Navigator.   Below is a clock applet that is embedded in the HTML code for this page.  

Your browser does not support Java so this clock applet cannot be seen!

The following notes walk you through:-

Creating Your First Application

Creating Your First Applet


Creating Your First Application

  1. Create a Directory for your Program Files

It is good practice to create a new folder for every Java program that you write. In this example I would create a folder called "HelloWorld".

  1. Write the Source Code

The next thing to do is to write the source code for your application. This simple example prints out the phrase "Hello World" at the command prompt. 

Using your favorite text editor such as Notepad or WordPad type in the following exactly.  Double-check your capitalization - Java is case-sensitive.

 /*  The HelloWorld class implements an application that simply displays "Hello World!" to the standard output.  */


public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!"); 
    }
}

The name of our Java source file must be the same as the name of the class followed by the ".java" extension. Therefore the file above must be named

HelloWorld.java

Save the source file you have created in your "HelloWorld" directory with the name HelloWorld.java

Note: --  When you save your file, make sure you save it as a text file. Do not save it as a WordPad document or any other format. 

Also make sure that the file you create does not have a ".txt" extension. For example, Notepad may have saved the file as HelloWorld.java.txt.  

You need to make sure it is correctly called HelloWorld.java

In some versions of Notepad, to avoid the ".txt" extension, you can simply surround the name of the file in quotation marks when you save the file.

         

  1. Compile the Source Code

So far you have created a .java source code file.  Your computer does not understand your source code so it has to be translated into code that your computer understands.  This translation process is called compiling.  

When you compile your source code it is changed into bytecode and stored in a .class file.  Every time you run your compiled code, the JVM (Java Virtual Machine) reads your bytecode from your .class file and translates this code into machine code that your computer can understand and run the code. 

Now lets compile your source code.

To compile your HelloWorld.java source code, open the command prompt window and change to your "HelloWorld" directory.

Note: --  If you are unsure about DOS commands then see the DOS page

There are also details on the cmdPromptHere utility on the Utilities page.  

 

At the command prompt type;-

javac HelloWorld.java

This will produce a ".class" file named "HelloWorld.class".   

If all goes well the system should pause for a minute and then return you to the prompt.  Now type the following;-

dir

You should get the following listing

>HelloWorld.java   HelloWorld.class

You are now ready to run your first application!!!!

Note: --  If you get any errors check your HelloWorld.java source code to make sure it looks exactly as written above.

If you get the error that javac was not found then your PATH variable has not been set

To fix this check out how to modify your PATH

 

  1. Run the HelloWorld Application

At the command prompt window type:-

java HelloWorld

You should see the following

Hello World!

If this is the case, congratulations! - You have just written your first Java application

 

Now follow the link for an explanation of the Hello World Application.

 


Creating Your First Applet

As mentioned earlier, applets are designed to run in a web browser, rarely can they be run as stand-alone applications.  We will be using both the Java AppletViewer tool and a browser to run an applet that you create.

In this exercise, we are going to create a HelloWorld applet.  This will be similar to the HelloWorld application you wrote but will run in a browser or AppletViewer

  1. Create a Directory for your Program Files

Create a directory called "HelloWorldApplet".

  1. Write the Source Code

The next thing to do is to write the source code for your applet. The procedure for compiling and running applets is very similar to that for applications. The first thing you must do is write your ".java" source file. 

Using your favorite text editor such as Notepad or WordPad type in the following exactly.  Double-check your capitalization - Java is case-sensitive.

/* 

The HelloWorldApplet class implements an applet that simply displays "Hello World!".
 */
import java.applet.Applet;
import java.awt.Graphics;

public class HelloWorldApplet extends Applet {
    public void paint(Graphics g) {
        g.drawString("Hello World!",0,50);
    }
}

Since the name of our Java source file must be the same as the name of the class followed by the ".java" extension the file must be saved as;-

HelloWorldApplet.java

Save the source file you have created in your "HelloWorldApplet" directory with this name. 

  1. Compile the Source Code

Open the command prompt window and change to your "HelloWorldApplet" directory.

 The compilation step is identical to compiling a Java application source file. So at the DOS command prompt

you must give the following command:

javac HelloWorldApplet.java

This will produce a ".class" file named "HelloWorldApplet.class". This contains the byte code that your browser or AppletViewer will use to execute your applet.

If all goes well the system should pause for a minute and then return you to the prompt.  Now type the following;-

dir

You should get the following listing

>HelloWorldApplet.java   HelloWorldApplet.class

You are now ready to run your first applet!

  1. Create a HTML File

Applets are meant to be included in HTML pages. this means you must create a HTML file that contains a reference to your applet.  The reference to your applet is created using an <Applet>tag which will run the applet you have just compiled.

Using the <APPLET> tag, you specify (at a minimum) the location of the applet and the dimensions of the applet's onscreen display area. When a Java-capable browser encounters an <APPLET> tag, it reserves onscreen space for the applet, loads the Applet into the browser.

Thus the tag must look something like this:

<applet code="HelloWorldApplet.class" width=100 height=100>

</applet>

 

Note: --  The applet tag is case sensitive.  So if you have used capital letters in the name of your class, you must use capital letters in the code tag.    

 

Using your favorite text editor such as Notepad or WordPad type in the following exactly.  

<HTML>
<HEAD>
<TITLE> My First Applet </TITLE>
</HEAD>
<BODY>
Here is the output of my program:
<APPLET CODE="HelloWorldApplet.class" WIDTH=250 HEIGHT=125>
</APPLET>
</BODY>
</HTML>

Save your HTML file into the same directory as your applet files.  That is to your "HelloWorldApplet" directory.

You can name it anything you like but "HelloWorldApplet.html" seems appropriate.

Now you have should have three files in this directory.

HelloWorldApplet.java     Your source code

HelloWorldApplet.class    Your compiled code

HelloWorldApplet.html     Your HTML code

Finally, to run your applet you can open the HTML file in a browser or run the AppletViewer from the DOS command line (preferred):

  1. Run the Applet using a Browser

Using Windows "My Computer" navigate to your  HelloWorldApplet directory and double-click your HTML file. The browser should load and display your applet

  1. Run the Applet using a AppletViewer

You can use the AppletViewer tool that comes with the JDK software to view your applets.  One reason for using this tool is that there is less overhead than using a browser.  The applet is loaded more quickly and so is useful in the code development stage.

Below is a screenshot of my HelloWorld applet running. 

At the command prompt type

appletviewer HelloWorldApplet.html

You should see   Hello World! displayed in the AppletViewer.

If this is the case, congratulations! - You have just written your first Java applet!


Now follow the link for an explanation of the Hello World Applet.

 

 

  Site Home 

Java Home   

  Forum  

Course Info

Welcome

Overview

Assessment

Qualification

Scheme of Work

Assignments

Resources

Information

Blackboard

Learning Center

Web Materials

Reading

Java Demos

Utilities

Links

Lecture Materials

Tutorials & Notes

Exercises

Activities

Quizzes

 

Site Home

Top

Unit Home

ADR 2002