Mac OS
Since Microsoft Windows is the most common platform we'll now look at the steps involved in installing the JDK on Windows
95/98/NT.
1) Download the JDK from Sun's website.
2) Place the downloaded executable in your root directory of your C Drive (C:\).
3) Run JDK-1_0_2-win32-x86.exe. This will extract the Java files to a directory called c:\java.
4) Open the file c:\autoexec.bat in Notepad. Add the following lines to the end of the
file:
SET PATH=%PATH%;C:\java\bin
SET CLASSPATH=C:\java\lib\Classes.zip
5) Restart your computer to allow the changes made to take affect.
The PATH variable tells Windows where to look for programs. Since we will need to access the Java compiler and virtual machine
from other directories on your computer, Windows needs to know where to find it.
The CLASSPATH variable tells the Java compiler where the standard class files that come with Java are located.
Now that you hopefully have the JDK installed we need to make sure that it is working. To do this we'll run through the six steps
required to create, compile and run a simple Java application. If you have any errors then have a read of the
trouble shooting section at the end of
this lecture.
Step 1:
Firstly you will need a place to store your applications so create a directory called JavaApps in c:\java. Next create a directory for your
first application called FirstApp in the JavaApp directory (c:\java\JavaApps\FirstApp).
The examples in this course are written using notepad but you can use any text editor as long as the files are saved as plain ASCII
text.
Start your text editor and enter the following text
class FirstJavaApp
{
1.
public static void main (String args[ ])
{
1.
System.out.println("Hello there!");
}
}
Don't worry what this means as this it will be explained in a later lecture. The purpose of this exercise is to make sure your compiler
and JVM work correctly.
Step 2:
Save the file as FirstJavaApp.java in the c: directory.
Note
: You should be careful to watch for Notepad placing an extra ".txt" at the end of the file name.
Next you have to compile you program in the text file into Java byte code:
Step 3:
Bring up an MS DOS console. This is generally done by going to Start -> Programs -> MS-DOS Prompt.
Step 4
: Go to the c: directory by typing the following at the console.
C:
cd
Step 5:
You now have to compile your program. So enter the following at the console.
javac FirstJavaApp.java
The program javac is the Java compiler that takes a text file as its input (as a parameter) and generates the corresponding byte-code.
The byte-code file has the same name as the entered text file but with a .class extension.
If everything has worked then after a few seconds the command line prompt should return and no errors should be shown. If an error is
generated carefully check your code and the commands entered at the command prompt and try again. Remember that Java is case
sensitive.
If every thing has gone right there should be the file FirstJavaApp.class in the directory
c:\Java\JavaApps\FirstApp. This is the byte code version of your program.
Now you have this amazing byte-code file that can be run on any operating system, but how do you actually run it on your machine?
Step 6
: Enter at the MS DOS command prompt:
java FirstJavaApp
You should see the words "Hello there" printed on the screen. As the following screen shot shows.

Congratulations, you've now written, compiled and run your first Java application! As mentioned in the introduction Java works great over
the Internet, the next section deals with writing your first Java applet. A Java applet is a program written in Java that will run in a
web browser
You will need a place to put your applets so create a directory called JavaApplets in c:java. Also, create the directory FirstApplet in this
new directory for the applet we are about to write (c:\java\JavaApplets\FirstApplet).
Step 1:
Start your text editor and enter the following text
import java.awt.Graphics;
public class FirstJavaApplet extends java.applet.Applet
{
public void paint(Graphics g)
{
g.drawString("Hello there!", 20, 25);
}
}
Step 2:
Save the file as FirstJavaApplet.java in the
c:\Java\JavaApplets\FirstApplet directory.
Step 3
: Go to the c:\Java\JavaApplets\FirstApplet directory by typing the following at the MS-DOS console:
C:
cd:\Java\JavaApplets\FirstApplet
Step 4
: Compile your applet by entering
javac FirstJavaApplet.java
Step 5:
If everything has worked then after a few seconds the command line prompt should return and no errors should be shown. If an error is
generated carefully check your code and the commands entered at the command prompt and try again. Remember that Java is case
sensitive.
Step 6:
There should now be the file in the c:\Java\JavaApplets\FirstApplet directory called FirstJavaApplet.class. This is the byte code version of your applet.
Like before, you have this byte-code file. However, before you can run it in a web browser you have to create a web page that gives the
browser information about your applet. To do this:
Step 7: Start your text editor and enter the following text
<HTML>
<HEAD>
<TITLE>My first Java applet</TITLE>
</HEAD>
<BODY>
This is my first Java applet
<BR>
<APPLET CODE="FirstJavaApplet.class" WIDTH=150 HEIGHT=50>
</APPLET>
</BODY>
</HTML>
Step 7:
Save the file as FirstJavaApplet.htm in the c: directory.
Step 8:
Load this file up into your favourite web browser and you should see the applet running.

Click here to go to a web page running this applet.
The help file containing detailed information about the different classes can be
downloaded here .
Unzip the file to a directory, for example
c:\jdk-1_0_2-apidocs\
The help file is not that much use in learning to program Java but when we get to looking at user interface controls it will become
more useful as a reference guide
.
Hopefully you have not had any problems installing the JDK or creating, compiling and running the examples. However, if you had take a
look to see if any of your problems are here
.
8.1.1 Class not found error
You getting are the error:
Exception in thread "main" java.lang.NoClassDefFoundError: FirstJavaApp

This error is generated when the JVM cannot find your program. To solve this you will have to add the directory
C:\Java\JavaApps\FirstApp
to the class path in your autoexec.bat file. This means altering the line
SET CLASSPATH=C:\java\lib\Classes.zip
to
SET CLASSPATH=C:\java\lib\Classes.zip; C:\jJava\JavaApps\FirstApp
Restart your machine and try again
.
You getting are the error:
Can't find class FirstJavaApp

This error is generated when the JVM cannot find your program. To solve this you will have to add the directory
C:
to the class path in your autoexec.bat file. This means altering the line
SET CLASSPATH=C:\java\lib\Classes.zip
to
SET CLASSPATH=C:\java\lib\Classes.zip; C:\Java\JavaApps\FirstApp
Restart your machine and try again
.
8.3.1 Web browser reports an error
Because Java has a high level of security built in some web browsers won't always run Java applets because they think that they might
be doing something they shouldn't. To get around this you can either lower the security level of your web browser or run the applet using
the applet viewer provided with the JDK. To use the applet viewer enter AppletViewer followed by the web page at the command prompt. As
shown in following example. A new window will now appear with your Java applet running inside it
.

In this lesson you were shown how to start programming in Java. This involved downloading the JDK, installing it and setting in up. You
wrote, compiled and ran your first Java application and Java applet, which ran in a web browser.
If you have got this far and the examples are working on your machine then you are well on the way to writing Java programs. Since this
course is Internet based we will be concentrating on Java applets and in the future lectures we'll cover topics such as creating a user
interface, reading and writing to files and networking
.
Menu: Home, Services, Events, Features, Interviews, Profiles, Reviews, News, Resources, Press