Installing Java & Writing, Compiling & Running a Java Program on a PC running MS Windows XP
Preamble
There is NO requirement on any of the programming courses run in this department for the student to provide their own computer; everything that you need is provided on the Sun UNIX Work Stations in Room 705 and the PCs in Room 610. However, many students wish to have Java on their own computer so that they may continue project development or course exercises at home. These notes are for those students. As the overwhelming majority are using PCs with the MS Windows XP operating system I have written these notes with that system in mind [January 2007]. The other MS Windows operating systems are similar. I indicate an appropriate link, at the relevant point, for Macintosh (Mac OS) and LINUX users.
1. Installing Java on your PC
1.1 Downloading the Java Development Kit from the Sun Java web site for a PC
A free version of Java may be obtained from the Sun Web Site
http://java.sun.com/javase/downloads/index.jsp
Make sure that you down load the Development Kit and not just the Run Time Environment (JRE). The download file name should have an dk. in it, e.g. an appropriate version of the Java development Kit, as a I write this (7 May 2007), would be the SE Development Kit (JDK), JDK 6u1, with dowload file titles, jdk-6u1-windows-i586-p-iftw.exe (on-line installation) or jdk-6u1-windows-i586-p.exe (off-line installation).
[Mac OS and LINUX users should follow the appropriate links on http://java.sun.com/docs/books/tutorial/getStarted/cupojava/]
1.2 Installing the Java Development Kit on your PC
Click on the downloaded executable file, e.g. jdk-6u1-windows-i586-p-iftw.exe (on-line installation) or jdk-6u1-windows-i586-p.exe, and follow the on-screen installation instructions. The Java Development Kit Folder (present [7 May 2007] update is called jdk1.6.0_01) will be installed in your C:\Program Files\Java folder.
1.3 Setting the PATH to the Java Development Kit on MS Windows XP
For ease of use, e.g. to be able to compile and run programs in any folder on your C drive with simple javac and java commands, you need to set the PATH. To do this for MS Windows XP, NT and 2000:
- Click on start;
- Click on Control Panel;
- Click on System;
- Click on Advanced tab;
- Click on Environmental Variables button;An Envionmental Variables Box will appear;
- Click on the Path line in the lower System variables window so that it is outlined in blue;
- Click on the edit button below the lower System variables window;An Edit System Variable Box will appear;
- If the Path line, now in the edit window, does not end in a semi-colon(;) add a semi-colon to the end of the lineType, at the end of the Path line, the address to the Java Development Kit bin, e.g. if it is in a folder named jdk1.6.0_01 and if this is stored in the Program Files Java folder type C:\Program Files\Java\jdk1.6.0_01\bin at the end of the Path line;
For different versions of the Develeopment Kit replace jdk1.6.0_01 with the appropriate folder name;
- Click on the OK, set or apply button in the Edit System Variable box;
- Click on the OK, set or apply button in the Environmental Variables box;
- Click on the OK, set or apply button in the System Properties (Advanced) box;
- To make the new Path effective type C:\autoexec.bat in the Command Prompt Window (See Using the Command Prompt Window below), i.e.
C:\>C:\autoexec.bat
or restart your PC.
If a Path to a Java Development Kit bin already exists amend it to correspond to your new downloaded folder.
2. Installing the flanagan library on your PC
2.1 Downloading the flanagan library from the Departmental Flanagan Library web site
You can download the flanagan library as a jar file, called flanagan.jar, from the web page http://www.ee.ucl.ac.uk/~mflanaga/java/index.html or directly from the following link: download flanagan.jar.
On clicking on this link a dialog box asking whether you want to open or save flanagan.jar will open. Click on save. You must save the file in the external library folder (ext) of the library folder (lib) of the Java Run Time Environment (JRE) folder (jre) in the Java Development Kit Folder, (e.g. jdk1.6.0_01), i.e. if you have downloaded and saved it in the Program Files Java folder you must save flanagan.jar in
C:\Program Files\Java\jdk1.6.0_01\jre\lib\ext
A jar format (Java Archive format) allows several files to be bundled into a single file. The flanagan Java library is all compressed into the flanagan.jar file. You do NOT need to extract any of the class files from this compressed file.
2.2 Setting the CLASSPATH to the flanagan library on MS Windows XP<
You must now set the CLASSPATH to the flanagan.jar file:
3. Making Folders for your Programs
3.1 Creating and Naming a Folder
It is sensible to make a folder (directory) for your Java programs. To create a new folder click on the C Drive icon, click on the File drop down menu, click on new, click on Folder. A new folder will appear on your C Drive called ‘New Folder’ which you can rename by overwriting the name ‘New Folder’. This folder may be called by any name you wish, e.g. JavaProg, and may be placed within other folders. However placing it directly on your C drive will simplify the change directory command in the command prompt window (See Using the Command Prompt Window below).
3.2 Using the Command Prompt Window
The equivalent of the UNIX terminal window on your PC is, if you are running MS Windows XP (or NT or 2000), the Command Prompt window. You will find this by:
- clicking on start;
- clicking on All Programs;
- clicking on Accessories;
- clicking on Command Prompt;
You may wish to create a shortcut on your desktop to the Command Prompt window. You do this by going to Command Prompt in the Accessories menu (as described above) and Right clicking on Command Prompt. A short cut will appear in the Accessories menu. You may drag this onto the desktop.
The equivalent window in the MS Windows 95 and 98 systems is the MS-DOS Prompt window.
In MS Windows XP the Command Prompt Window opens in your home directory, e.g. C:\Documents and Settings\Michael T Flanagan>
The change directory command is cd, e.g. to change to a folder (directory) on the C drive called JavaProg type
cd c:\JavaProg
after the prompt symbol (>) in the Command Prompt window followed by pressing the enter (return) key.
4. Writing your Program
You may use any text editor, e.g. Notepad, Wordpad or Word, to write your Java source code. However you must save it with the extension .java and as a text file, i.e. using the:
- Text Documents [*.txt] save option in NotePad;
- Text Document save option in WordPad;
- Plain Text save option in Word;
5. Compiling your Java Program
The compilation commands are identical to those that you use on the UNIX work stations, i.e.to complile a source code, e.g. called MyFirstJavaApplication.java, type, after the last prompt, >, in the Command Prompt window
javac MyFirstJavaApplication.java
If successful, a class file called MyFirstJavaApplication.class will appear in the relevant folder, e.g. JavaProg.
Check any error messages and go back to the text editor to make changes if required then save and compile file again.
NB Always save the most recent version before compiling!
4. Running your Program
4.1 Running a Java Application
Again, the commands are the same as those that you are using on the Sun UNIX Work Stations. If you have compiled a Java source code, e.g. called MyFirstJavaApplication.java, for a Java application you should have a bytecode file MyFirstJavaApplication.class in the same folder. You can now run this application by typing the command:
java MyFirstJavaApplication
in the Command Prompt window.
4.2 Running a Java Applet
If you have compiled a Java source code, e.g. called My FirstApplet.java, for a Java applet you should have a bytecode file MyFirstApplet.class in the same folder.
You need an html file to execute this file, e.g.
The width and height are the dimensions of the window, in pixels, in which the applet will be run on the web page.
You may use any text editor, e.g. Notepad, Wordpad or Word, to write your html code. However you must save it with the extension .html and as a text file, i.e. using the:
You may only run the applet using a web browser. There is now appletviewer avaiable on a PC.
You may:
KOMENTAR