This lab assignment is designed to familiarise you with the process of creating Java programs using the JDK (Java Development Kit), Notepad and the Command_Prompt under Microsoft's Windows XP operating system. You will use the standard Java compiler, "javac," the Java Virtual Machine, "java," and the applet browser, "appletviewer" to create Java programs.
Reminder: The Java compiler is a computer program which takes, as input, program source code (written in the Java programming language) and converts it into Java byte-code. This is then used as input to the Java Virtual Machine (JVM, another computer program) which converts it to machine code and executes it instruction by instruction. The appletviewer (and web browsers, which include their own JVM) execute a sub-class of Java programs, called applets, and also require an html webpage in which to display the applet.
Note: This lab assumes that your system is set up correctly. You will need to have the jdk (available free from Sun at http://java.sun.com) installed on your computer. If either java or javac fail to run, check that the path variable is set correctly by typing "set" at the COMMAND PROMPT. The line starting with "PATH" should include the actual path to java.exe on your system (usually something like C:\Program Files\Java\jdk1.5.0_04\bin ) If it doesn't, type "set path=%path%;C:\Program Files\Java\jdk1.5.0_04\bin".) The other problem you may face concerns the compiler not locating the necessary java class files. You can use the "set" command again to see and change the default CLASSPATH environment cariable as necessary (it should normally include C:\Program Files\Java\jdk1.5.0_04\lib and the current directory ".") Type "set classpath=.;C:\Program Files\Java\jdk1.5.0_04\lib" to ensure this is set correctly. Note: the version number/folder name of the JDK may be slightly different on your machine, in which case you should change the set commands accordingly!
Carefully read and follow the steps below...
Your First Java Programs
The following assumes you are using a floppy disk in the A: drive. Alternatively, you may use your networked H: drive or a USB Flash drive by substituting the appropriate drive letter/path name.
Creating a Java Console Application program…
Open NotePad (Start|Programs|Accessories|NotePad) and carefully copy/paste program No.1, below, into it. Save it to your floppy disk as "Application.java"
Tip: To do this, go to the file menu and click on Save As… You will see a file save dialog box open. In the Save in box, select "31/2 floppy disk (A:)" then, in the File name box, type Application.java, then click the Save button. Note: to see your Java files in NotePad file dialog boxes, you will need to select "All Files (*.*)" in the Save as type box.
Compiling and running it…
Run the Windows CommandPrompt by selecting Start|Programs|Command_Prompt. A black window will open and you should see the Command prompt, probably C:\Documents and Settings\your_name>
Type A: (it should appear next to the Command prompt) and then press enter. The prompt should change to A:\>
Now you are ready to compile your first java program. Type
javac Application.java
at the command prompt. Assuming there were no syntax errors, this should create a new file named "Application.class" on your diskette (be patient, it may take a minute or two!) Typing dir at the command prompt will give a list of all the files on your diskette. If the compilation reported errors then see the section below on "Correcting Syntax Errors" before continuing to run your program.
Providing your program has compiled without any errors, then you can execute it by typing,
java Application
The program should now run and prompt you to enter your name. Go ahead, type it in. Hopefully, you got a Java greeting in reply. Neat eh?
Creating and running "Java Applets"
Using NotePad again, copy/paste program 2, below, and save it on your floppy disk in a file called "AnApplet.java" Using NotePad yet again, copy/paste the HTML file, below, and save it on your floppy disk in a file called "MyApplet.html"
From the MSDOS Prompt, compile the program as before, by typing,
javac AnApplet.java
To run Java applets, you should use the appletviewer (instead of the JVM), by typing,
appletviewer MyApplet.html
You should see a small window open up and display a saying from Confucius.
These sort of Java programs are called "Applets". The most important feature of an applet is that it can be executed using a web browser (like Firefox or Internet Explorer). Try it now. Open your web browser and, from the "File" menu, select "Open File" and browse to & select the file MyApplet.html which you created earlier. Click "Open" and you should now see how the applet running in the web browser window. Note the "My first Applet!" sentence on the title bar. This is the sentence you wrote between the <title> tags in MyApplet.html. Try clicking on the "The Source" link and see what is displayed.
Correcting Syntax Errors
The chances are that you will make errors while typing your program into NotePad. If you did, the compiler will have reported one or more syntax errors! Before you can run your program, you must fix these. If by any chance you didn't experience any errors during compilation, then introduce one now by deleting a semicolon from the end of one of the lines, then save and re-compile the program. This time it will not compile correctly because of the syntax error you introduced! The error message will probably read "; expected." and tell you the line number on which the problem occurred. Unfortunately, error messages are not always very clear (for example, sometimes the same error may result in "Invalid declaration" instead!) and you thus have to be a bit of a detective to find the real source of the problem. Anyway, go ahead and correct any errors using NotePad, making sure that you remember to save your updated program before attempting to recompile it again.
Staying Neat & Tidy!
In the preceding exercises you created all the files in the root folder of your floppy disk. Usually, it is better to keep things neat and tidy by putting each exercise/project/assignment/etc., in its own folder. Use the Windows Explorer program to create new folders (with meaningful names!) and move the files you created above into them. In order to compile and run them you will need to change to the appropriate folder (directory) in the CommandPrompt window. To do this type "cd directory_name" and press enter. To go back to a parent folder type "cd .." (then enter as always!) For more information on MSDOS commands see the appropriate optional lab assignment!
Your Assignment
Try modifying each program so it prints out a personalised message. Demonstrate to the assistant that you can compile and run the console application and the applet, and that you can fix syntax errors, as necessary. Rather than using NotePad, try using "edit" from the CommandPrompt. Doing everything in one window will give you a better feeling for the edit, compile, run cycle (and help you appreciate the advantages of a IDE -Integrated Development Environment- such as JCreator).
Program 1: Console Application
// A simple console application which reads your name
// and prints it preceded by the word hi.
// Author: David Davenport
import java.io.*;
public class Application
{
public static void main( String[ ]
args) throws IOException
{
String yourName;
BufferedReader kb = new
BufferedReader(
new InputStreamReader( System.in) );
System.out.println( "Type your
name and press enter");
yourName = kb.readLine();
//print the string together with
hi
System.out.println( "Hi " +
yourName);
}
} // end class Application
Program 2: Applet
import java.applet.Applet;
import java.awt.*;
//**************************************************
// AnApplet.java
// Authors: Lewis and Loftus
//**************************************************
public class AnApplet extends Applet
{
public void paint
(Graphics page)
{
page.drawString( "Forget
injuries, never forget kindness.", 50, 50);
page.drawString( "--
Confucius", 70, 70);
}
} // end class AnApplet
HTML file:
<html>
<head>
<title>My first Applet!</title>
</head>
<body>
<hr>
<p>A quotation brought to you by yourname
</p>
<applet code=AnApplet.class width=200
height=200></applet>
<hr>
<a href="AnApplet.java">The source.</a>
</body>
</html>